Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > class attribute to instance attribute

Reply
Thread Tools

class attribute to instance attribute

 
 
Donnal Walter
Guest
Posts: n/a
 
      06-30-2005
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


 
Reply With Quote
 
 
 
 
Devan L
Guest
Posts: n/a
 
      06-30-2005
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?

 
Reply With Quote
 
 
 
 
Donnal Walter
Guest
Posts: n/a
 
      07-01-2005
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


 
Reply With Quote
 
Devan L
Guest
Posts: n/a
 
      07-01-2005
Well, I've never heard of a method like that for assigning variables.
I'd rather put it in the __init__ method.

 
Reply With Quote
 
Greg Ewing
Guest
Posts: n/a
 
      07-06-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class object's attribute is also the instance's attribute? 陈伟 Python 9 08-30-2012 03:20 PM
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
Implied instance attribute creation when referencing a class attribute Russell Warren Python 5 01-17-2006 05:07 AM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments