Initializing an attribute that needs the object

D

David Pratt

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)
 
D

David Pratt

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
 
J

John Machin

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 ummmm ...
See this: http://www.python.org/doc/newstyle/

HTH,
John
 
B

Bruno Desthuilliers

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)
 
D

David Pratt

Hi John. Thank you for the tips and the link. This is helpful. Many thanks.

Regards
David
 
B

Bruno Desthuilliers

David Pratt a écrit :
<meta>
David, please, don't top-post (fixed)
Bruno Desthuilliers wrote:
(snip)
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,536
Members
48,283
Latest member
SherriP988

Latest Threads

Top