Initializing an attribute that needs the object

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Jun 3 13:02:58 EDT 2006


David Pratt a écrit :
<meta>
David, please, don't top-post (fixed)
</meta>
> 
> Bruno Desthuilliers wrote:
> 
(snip)
>>
>> Hint : Python classes are objects too.
>>
>>
>> class Factory(object):
>>    def __init__(self, handler_class):
>>      self.handler = handler_class(self)
>>
>> class SomeHandler(object):
>>    def __init__(self, factory):
>>      self.factory = factory
>>
>> f = Factory(SomeHandler)

> Hi Bruno. This is certainly what I was missing. Thank you. I am afraid I 
> am behind the times with use of object. Will I only use object when I am 
> not subclassing? 

If you subclass from a new-style class (almost all classes in the 
standard lib are...), you don't need to anything more.Else, yes, inherit 
from 'object', or set 'type' as the metaclass. Both classes defined 
below are 'new-style' classes:

class Parrot:
   __metaclass__ = type

class CheeseChop(object):
   pass


And now:

class DeadParrot(Parrot):
   pass

is a new-style class too.


 > Where will I find a document that provides info on the
 > use of object in new style classes?

In the Fine Manual(tm), of course. Googling python.org for "type 
unification" or "new-style" should give relevant answers.



More information about the Python-list mailing list