class attribute to instance attribute

D

Donnal Walter

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

Devan L

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

Donnal Walter

Devan said:
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
 
D

Devan L

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

Greg Ewing

Donnal said:
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.
 

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
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top