type object of an instance without instantiation?

Gordon McMillan gmcm at hypernet.com
Sat Nov 6 13:35:47 EST 1999


claudius writes:

> I have the following code (well, in summary):
> 
> class BigClass:
>  def __init__(self):
>   ...does some horrifyingly nasty operation...
> 
> def some_generic_fn(object):
>  if hasattr(object, '__class__') and object.__class__ ==
>  BigClass:
>   ...do some BigClass-specific thing...
> 
> Is there a way to create a type object for the instance of a
> BigClass object without actually creating an instance of one?

The type object of an instance (any instance) is the one and 
only InstanceType object. The type of object of a class (any 
class) is the one and only ClassType object.

I think you mean the BigClass class object. This exists as 
soon as the "class" statement is executed. When you create 
an instance of BigClass, you are "calling" the BigClass class 
object.

BTW, while doing lots of stuff in the constructor is often a 
valuable technique in other languages, it is much less so in 
Python. It makes things like pickling a lot harder (unless you 
provide default args and alternate ways to get the work done).
That doesn't mean it's *always* bad, just that it reduces your 
flexibility.

- Gordon




More information about the Python-list mailing list