By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,942 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,942 IT Pros & Developers. It's quick & easy.

class attribute to instance attribute

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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?

Jul 19 '05 #2

P: n/a
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
Jul 19 '05 #3

P: n/a
Well, I've never heard of a method like that for assigning variables.
I'd rather put it in the __init__ method.

Jul 19 '05 #4

P: n/a
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
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.