Initializing an attribute that needs the object

David Pratt fairwinds at eastlink.ca
Fri Jun 2 23:45:24 CEST 2006


Hi Marco. Thanks for your reply. I am providing the handler with the 
factory instance as I have shown. This is how my code currently works. 
What I am trying to figure out is how to possibly provide the handler in 
the constructor when it needs the factory instance. This would give me 
some better flexibility.

ie.

class Factory

	def __init__(self, factory):
  		
At this point I don't have self. Would super help me?

Regards,
David


Marco Giusti wrote:
> On Fri, Jun 02, 2006 at 06:15:28PM -0300, David Pratt wrote:
>> Hi. I want to have different handlers to do perform logic. The problem 
>> is the Handler requires an instance of the factory since it will use its 
>> own methods in conjunction with methods of the factory.
>>
>> Once I have got a Factory instance I can give it a new handler (see 
>> below). It would be more flexible if I could provide a handle in 
>> constructor - but how to do this when it requires the object itself. 
>> Would I use a super for this sort of thing? Many thanks
> 
> when __init__ is called the object already exists.
> 
>> class Factory:
>>
>> 	def __init__(self):
>> 		self.some_handler = Handler(self)
>>
>> f = Factory()
>> f.some_handler = AnotherHandler(f)
> 
> try this, should works:
> 
> class Factory:
> 
>     def __init__(self):
>         self._some_handler = AnotherHandler(self)
> 
> maybe a class hierarchy is good for you
> 
> ciao
> m.
> 



More information about the Python-list mailing list