Re: Initializing an attribute that needs the object
David Pratt a écrit : David, please, don't top-post (fixed) > > 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Initializing an attribute that needs the object
Hi John. Thank you for the tips and the link. This is helpful. Many thanks. Regards David > A new-style class is one which inherits ultimately from the type that is > called "object". > > class NewStyleClass(object): > pass > > class OldStyleClass(): > pass > > Docs are a bit u ... > See this: http://www.python.org/doc/newstyle/ > > HTH, > John > -- http://mail.python.org/mailman/listinfo/python-list
Re: Initializing an attribute that needs the object
On 3/06/2006 9:17 AM, David Pratt **TOPPOSTED**: > 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? More precisely, don't use object when you are subclassing. > Where will I find a document that provides info on the > use of object in new style classes? > Other way up :-) A new-style class is one which inherits ultimately from the type that is called "object". class NewStyleClass(object): pass class OldStyleClass(): pass Docs are a bit u ... See this: http://www.python.org/doc/newstyle/ HTH, John -- http://mail.python.org/mailman/listinfo/python-list
Re: Initializing an attribute that needs the object
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? Where will I find a document that provides info on the use of object in new style classes? Many thanks. Regards, David Bruno Desthuilliers wrote: > David Pratt a écrit : >> 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. > > Hint : Python classes are objects too. > >> class Factory: > > Do yourself a favour : use new-style classes. > > 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) -- http://mail.python.org/mailman/listinfo/python-list
Re: Initializing an attribute that needs the object
David Pratt a écrit : > 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. Hint : Python classes are objects too. > class Factory: Do yourself a favour : use new-style classes. 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) -- http://mail.python.org/mailman/listinfo/python-list
Re: Initializing an attribute that needs the object
My apologies. What I meant to write was this class Factory def __init__(self, handler): David Pratt wrote: > 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. >> -- http://mail.python.org/mailman/listinfo/python-list
Re: Initializing an attribute that needs the object
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. > -- http://mail.python.org/mailman/listinfo/python-list
Re: Initializing an attribute that needs the object
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. signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
Initializing an attribute that needs the object
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 Regards, David class Factory: def __init__(self): self.some_handler = Handler(self) f = Factory() f.some_handler = AnotherHandler(f) -- http://mail.python.org/mailman/listinfo/python-list