Initializing an attribute that needs the object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Pratt

    #1

    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_handl er = Handler(self)

    f = Factory()
    f.some_handler = AnotherHandler( f)

  • Bruno Desthuilliers

    #2
    Re: Initializing an attribute that needs the object

    David Pratt a écrit :[color=blue]
    > 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.[/color]

    Hint : Python classes are objects too.
    [color=blue]
    > class Factory:[/color]

    Do yourself a favour : use new-style classes.

    class Factory(object) :
    def __init__(self, handler_class):
    self.handler = handler_class(s elf)

    class SomeHandler(obj ect):
    def __init__(self, factory):
    self.factory = factory

    f = Factory(SomeHan dler)

    Comment

    • David Pratt

      #3
      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:[color=blue]
      > David Pratt a écrit :[color=green]
      >> 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.[/color]
      >
      > Hint : Python classes are objects too.
      >[color=green]
      >> class Factory:[/color]
      >
      > Do yourself a favour : use new-style classes.
      >
      > class Factory(object) :
      > def __init__(self, handler_class):
      > self.handler = handler_class(s elf)
      >
      > class SomeHandler(obj ect):
      > def __init__(self, factory):
      > self.factory = factory
      >
      > f = Factory(SomeHan dler)[/color]

      Comment

      • John Machin

        #4
        Re: Initializing an attribute that needs the object

        On 3/06/2006 9:17 AM, David Pratt **TOPPOSTED**:[color=blue]
        > 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?[/color]

        More precisely, don't use object when you are subclassing.
        [color=blue]
        > Where will I find a document that provides info on the
        > use of object in new style classes?
        >[/color]

        Other way up :-)
        A new-style class is one which inherits ultimately from the type that is
        called "object".

        class NewStyleClass(o bject):
        pass

        class OldStyleClass() :
        pass

        Docs are a bit ummmm ...
        See this: http://www.python.org/doc/newstyle/

        HTH,
        John

        Comment

        • David Pratt

          #5
          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

          [color=blue]
          > A new-style class is one which inherits ultimately from the type that is
          > called "object".
          >
          > class NewStyleClass(o bject):
          > pass
          >
          > class OldStyleClass() :
          > pass
          >
          > Docs are a bit ummmm ...
          > See this: http://www.python.org/doc/newstyle/
          >
          > HTH,
          > John
          >[/color]

          Comment

          • Bruno Desthuilliers

            #6
            Re: Initializing an attribute that needs the object

            David Pratt a écrit :
            <meta>
            David, please, don't top-post (fixed)
            </meta>[color=blue]
            >
            > Bruno Desthuilliers wrote:
            >[/color]
            (snip)[color=blue][color=green]
            >>
            >> Hint : Python classes are objects too.
            >>
            >>
            >> class Factory(object) :
            >> def __init__(self, handler_class):
            >> self.handler = handler_class(s elf)
            >>
            >> class SomeHandler(obj ect):
            >> def __init__(self, factory):
            >> self.factory = factory
            >>
            >> f = Factory(SomeHan dler)[/color][/color]
            [color=blue]
            > 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?[/color]

            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(obje ct):
            pass


            And now:

            class DeadParrot(Parr ot):
            pass

            is a new-style class too.

            [color=blue]
            > Where will I find a document that provides info on the
            > use of object in new style classes?[/color]

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

            Comment

            Working...