[wxPython-users] Default wx.Choice() Value?

Christopher Barker Chris.Barker at noaa.gov
Fri Dec 22 13:43:31 PST 2006


Andrea Gavana wrote:

> Setters and Getters instead of Properties is just a matter of taste.

Yes, though some argument could be made for so called "pythonic style"

> wxPython now comes with a full (? not sure) set of properties tha
> "maps" the setters and getters (sorry, I might have said it in an
> horrible english), i.e.:

> Personally, I find the "property" approach fantastically confusing,

I'm not sure you would if you had started with it.

> In any case, I prefer to leave
> properties as something that I assign, i.e.:
> 
> MainFrame.installDir = os.getcwd()
> 
> and using wxPython setters and getters to assign/retrieve wxPython things.

Well, using the Getter and Setters that wxPython provides is different 
than writing your own. wxPython started as a thin wrapper around C++, so 
that's the style it has. I'm not sure if/when I'll switch to using the 
wxPython properties, but for the most part, I avoid getters and setter 
when writing my own classes. My logic is thus:

Many of the attributes of a typical class are very straightforward -- 
they hold a value. Sometimes client code needs to know what that value 
is (getter), and sometimes it needs to set that value (setter). In the 
simple case, in Python, you just make it an attribute:

class A:
     def __init__(self):
         self.value = AdefaultValue


And you're done. Indeed, you don't even need to set a default, you can 
just assign it when you need it.

However sometimes, when an attribute is changed, something else in the 
class needs to change also  or the value ends up being stored somewhere 
else, in a widget, in a database, whatever. Now we need a setter, so 
that a method can be run that does whatever else needs to be done in 
that class. If this happens in a re-factor, all your client code now 
needs to change:

Object.value = 5

to

Object.SetValue = 5

Even if it's not a refactor, then your client needs to know which 
attributes can just be assigned, and which need Setters.

One solution: Make everything use a Getter and Setter! Now you write a 
lot of code like this:

class A:
     def __init__(self):
         self.value = AdefaultValue

     def GetValue(self):
         return self.value

     def SetValue(self, val):
         self.value = val


You're now writing this "just in case" you need to put something 
meaningful in one of those some day, and/or to provide a consistent 
interface. If you have ten attributes in a class, that's twenty 
do-nothing methods.

With properties, you can hide all that -- provide a consistent 
interface, and only write getters and setters if you need to. It makes 
the client code cleaner looking too:

Object.value = 5

Avalue = Object.Value

I like that style better.

-Chris




-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the wxpython-users mailing list