class attribute to instance attribute

Discussion in 'Python' started by Donnal Walter, Jun 30, 2005.

  1. This is a question about Python patterns or idioms. Over a period of
    time, I have evolved a pattern of usage that seems to work better for me
    than other ways I tried previously, but in writing some documentation I
    don't know what to call this syntax or how best to describe it. I have
    not seen it used in other places.

    The somewhat longish version is that I have implemented an MVP
    (model-view-presenter) architecture where each "presenter" has a "view"
    as an attribute. Take the TextField and Frame presenters for example.
    Here is one way to do it:

    import wx
    class TextField:

    def __init__(self):
    # do some things here to calculate *args
    self.view = wx.TextCtrl(*args)
    # do more things to set up the view

    # and more methods to make this a presenter


    class Frame:

    def __init__(self):
    # do some things here to calculate *args
    self.view = wx.Frame(*args)
    # do more things to set up the view

    # and more methods to make this a presenter


    There are a number of presenters, some of which have the same type of
    view (TextField and NumberField, for example) and many of which have
    different views (Frame and Notebook, for example). The way I have chosen
    to do this is to put the logic in one superclass, "Presenter":

    class Presenter:
    view = None

    def __init__(self):
    # do some things here to calculate *args
    # if view is class, create instance
    if callable(self.view):
    self.view = self.view(*args)
    # do more things to set up the view

    # and more methods to make this a presenter


    class TextField(Presenter):
    view = wx.TextCtrl

    class Frame(Presenter):
    view = wx.Frame


    Then:
    >>> app = wx.App(False)
    >>> f = Frame()
    >>> isinstance(f.view, wx.Frame)

    True

    To summarize, each subclass has a class attribute "view" that is
    converted to an instance attribute of the same name at runtime.

    Is this a common Python idiom? If so, does it have a name? Is there a
    better way to do the same thing?

    Regards,
    Donnal Walter
    Arkansas Children's Hospital
     
    Donnal Walter, Jun 30, 2005
    #1
    1. Advertising

  2. Donnal Walter

    Devan L Guest

    Why make it an instance attribute? Couldn't you just look at the class
    attribute? If its something that depends on each instance's value
    assigned to the attribute, why not make it an instance attribute to
    start with?
     
    Devan L, Jun 30, 2005
    #2
    1. Advertising

  3. Devan L wrote:

    > Why make it an instance attribute? Couldn't you just look at
    > the class attribute?


    Each "presenter" (instance) needs its own "view" (instance). The class
    attribute references a wxPython class. The resulting instance attribute
    references a wxPython object (widget or container).

    > If its something that depends on each instance's value
    > assigned to the attribute, why not make it an instance attribute to
    > start with?


    The view instance is not known at design time; it can only be created at
    runtime. To do this requires doing so in the __init__() method, either
    in a separate version method for every different presenter class, or
    once in the superclass, as I have done.

    Donnal
     
    Donnal Walter, Jul 1, 2005
    #3
  4. Donnal Walter

    Devan L Guest

    Well, I've never heard of a method like that for assigning variables.
    I'd rather put it in the __init__ method.
     
    Devan L, Jul 1, 2005
    #4
  5. Donnal Walter

    Greg Ewing Guest

    Donnal Walter wrote:

    > Each "presenter" (instance) needs its own "view" (instance). The class
    > attribute references a wxPython class. The resulting instance attribute
    > references a wxPython object (widget or container).


    This is a reasonable idea, but it would be less confusing
    to give the class variable a different name, such as
    'view_class'.

    I'm not aware of any specific name for this pattern. I
    suppose it could be regarded as an instance of
    data-driven programming -- you're putting a piece of
    data in the class that describes what is to be done,
    instead of writing code to do it.

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
     
    Greg Ewing, Jul 6, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sridhar R
    Replies:
    14
    Views:
    1,457
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Gerry Sutton
    Replies:
    1
    Views:
    585
    Peter Otten
    Apr 16, 2005
  3. Russell Warren
    Replies:
    5
    Views:
    496
    Russell Warren
    Jan 17, 2006
  4. Martin P. Hellwig
    Replies:
    1
    Views:
    389
    Martin P. Hellwig
    Mar 26, 2010
  5. 陈伟
    Replies:
    9
    Views:
    279
    Hans Mulder
    Aug 30, 2012
Loading...

Share This Page