refering to base classes

Georg Brandl g.brandl-nospam at gmx.net
Wed Aug 30 08:07:07 EDT 2006


Chaz Ginger wrote:
> glenn wrote:
>>> Shouldn't that be
>>>
>>> beagle = animal.dog()
>>>
>>> to create an instance?
>>>
>>> We've all done it ...
>> lol - actually Im confused about this - there seem to be cases where
>> instantiaing with:
>> instance=module.classname()
>> gives me an error, but
>> instance=module.classname
>> doesnt - so I got into that habit, except for where I had a constructor
>> with parameters - except now Im feeling foolish because I cant
>> replicate the error - which suggests I didnt understand the error
>> message properly in the first place... arrgh
>> I guess thats just part of the process of gaining a new language.
>> 
>> glenn
>> 
> 
> module.classname and module.classname() are two different things. If you 
> use module.classname() you invoke the __new__ and __init__ methods in 
> the class, and you might get an error from them.
> 
> On the other hand module.classname will always work, assuming classname 
> really exists in module. What you get back is a sort of reference to the 
> class itself and not an instance of it.

It is not a sort of reference to the class, it is *the class itself*.

 >>> class A:
...     pass
...
 >>> A
<class __main__.A at 0xdeadbeef>
 >>>

Georg



More information about the Python-list mailing list