[wxPython] Experimenting...

I've been experimenting with adding properties to wxPython. (Properties are
attributes that magically turn into Get or Set method calls.) They let you
do things like

    print window.size
    window.size = (300,200)

instead of

    print window.GetSize()
    window.SetSize( (300,200) )

It acutally turned out to be a bit easier than I was expecting. It is even
possible for classes implemented in Python to add their own properties or
override those in the base classes.

A potential problem is name clashes. (The demo has a few already.) It can
be a bit suprizing sometimes that assigning something to self.sizer doesn't
actually save a local reference to the something but calls a method instead.
Especially if it worked fine yesterday before the properties and acts
wierdly today...

So what's your opinion on properties? Is the syntactic sugar worth the
potential hassles? Should it wait until there is Python specific
documentation so you don't have to search the sources to know what names are
magic?

FYI, here are the properties for wxWindow and the methods that would be
called to access or set them:

    _prop_list_ = {
        'size' : ('GetSize', 'SetSize'),
        'enabled' : ('IsEnabled', 'Enable'),
        'background' : ('GetBackgroundColour',
'SetBackgroundColour'),
        'foreground' : ('GetForegroundColour',
'SetForegroundColour'),
        'children' : ('GetChildren', None),
        'charHeight' : ('GetCharHeight', None),
        'charWidth' : ('GetCharWidth', None),
        'clientSize' : ('GetClientSize', 'SetClientSize'),
        'font' : ('GetFont', 'SetFont'),
        'grandParent' : ('GetGrandParent', None),
        'handle' : ('GetHandle', None),
        'label' : ('GetLabel', 'SetLabel'),
        'name' : ('GetName', 'SetName'),
        'parent' : ('GetParent', None),
        'position' : ('GetPosition', 'SetPosition'),
        'title' : ('GetTitle', 'SetTitle'),
        'style' : ('GetWindowStyleFlag',
'SetWindowStyleFlag'),
        'visible' : ('IsShown', 'Show'),
        'shown' : ('IsShown', 'Show'),
        'toolTip' : ('GetToolTip', 'SetToolTip'),
        'sizer' : ('GetSizer', 'SetSizer'),
        'validator' : ('GetValidator', 'SetValidator'),
        'dropTarget' : ('GetDropTarget', 'SetDropTarget'),
        'caret' : ('GetCaret', 'SetCaret'),
        'autoLayout' : ('GetAutoLayout', 'SetAutoLayout'),
        'constraints' : ('GetConstraints', 'SetConstraints'),
    }

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com
http://wxpython.org Java give you jitters?
http://wxpros.com Relax with wxPython!

So what's your opinion on properties? Is the syntactic sugar worth the
potential hassles? Should it wait until there is Python specific
documentation so you don't have to search the sources to know what names

are

magic?

   Properties are very cool for writing more obvious code like:

sci.zoom = sci.zoom + 1
if sci.selectedText == "def":
    sci.selectedText = "function"

   Another positive is to lessen the number of names a user has to see or
remember.

   I've been adding extra methods to Scintilla to help in an environment
that supports properties. Wherever possible, there should be getters as well
as setters. Its also good to use a naming convention like Get*,Set* as it
helps both users and automated tools.

   Neil

Robin Dunn wrote:

I've been experimenting with adding properties to wxPython. (Properties are
attributes that magically turn into Get or Set method calls.)

<snip>

So what's your opinion on properties? Is the syntactic sugar worth the
potential hassles? Should it wait until there is Python specific
documentation so you don't have to search the sources to know what names are
magic?

I think properties sound like a very nifty idea. Would the methods also
work?

As for documentation, it wouldn't require the Python specific docs, just
a note in the Get and Set method documentation. This would be a lot of
notes, but not as big a deal as a whole separate set of docs.

-Chris

···

--
Christopher Barker,
Ph.D.
cbarker@jps.net --- --- ---
http://www.jps.net/cbarker -----@@ -----@@ -----@@
                                   ------@@@ ------@@@ ------@@@
Water Resources Engineering ------ @ ------ @ ------ @
Coastal and Fluvial Hydrodynamics ------- --------- --------
------------------------------------------------------------------------
------------------------------------------------------------------------

My answer to this would very much depend on the mechanism you intend to
use to implement this feature. For instance, doing it on the Python side
with __getattr__ and __setattr__ methods seems hardly acceptable
because:

1) the calls to __getattr__ and __setattr__ would add more overhead (and
in my
   experience this can get substantial; to name just one example,
harmless
   expressions like <wxWindow instance> == None will result in a call to
   __getattr__ of <wxWindow instance> with "__cmp__" as argument);

2) while I do use magic attributes in my own classes occasionally, I
like the
   "bricks" I am building my classes with (i.e., the wxPython classes)
to be
   simple and as "un-magical" as possible - it is awkward, not to
mention the
   yet another layer of overhead, if you have to call
__getattr__/__setattr__
   of your base class (plus you get mysterious core dumps if you forget
to do it!).

On the plus side of this whole exercise, we only save a little typing
(which I don't mind) - or am I missing something?

Oliver

···

wxpython-users-admin@wxwindows.org wrote:

I've been experimenting with adding properties to wxPython. (Properties are
attributes that magically turn into Get or Set method calls.) They let you
do things like

    print window.size
    window.size = (300,200)

instead of

    print window.GetSize()
    window.SetSize( (300,200) )

So what's your opinion on properties? Is the syntactic sugar worth the
potential hassles? Should it wait until there is Python specific
documentation so you don't have to search the sources to know what names are
magic?

--
F. Oliver Gathmann
CRI Inc., 80 Ashford St., 02134 Boston, MA, USA
phone: (617) 787-5700#224, fax: (617) 787-4488
e-mail: ogathmann@cri-inc.com

Robin Dunn wrote:

I've been experimenting with adding properties to wxPython. (Properties are
attributes that magically turn into Get or Set method calls.) They let you
do things like

    print window.size
    window.size = (300,200)

instead of

    print window.GetSize()
    window.SetSize( (300,200) )

It acutally turned out to be a bit easier than I was expecting. It is even
possible for classes implemented in Python to add their own properties or
override those in the base classes.

A potential problem is name clashes. (The demo has a few already.) It can
be a bit suprizing sometimes that assigning something to self.sizer doesn't
actually save a local reference to the something but calls a method instead.
Especially if it worked fine yesterday before the properties and acts
wierdly today...

Maybe starting property names with a capital letter might lessen the
name clashes? This will make the properties look more wxWindowish
and more distinguishable from normal attributes (usually lowercase)

So what's your opinion on properties? Is the syntactic sugar worth the
potential hassles? Should it wait until there is Python specific
documentation so you don't have to search the sources to know what names are
magic?

I love properties!

I believe the pros outweigh the cons. Getters and setters are clumsy.
Conceptually you are setting an attribute even tho you have to call a
method to get it set.
Syntactic sugar sweetens life.

If you've got some code where you don't want the overhead of the
__getattr__/__setattr__ calls call the getter and setter directly
by all means, but in most cases it's not going to matter that
much.

A big facet of Python is the legibility which properties definitely
improves.

···

--
Riaan >>> a='a=%s;a%%`a`';a%`a`
___________________________________________________
Boa Constructor - RAD GUI building IDE for wxPython
     http://boa-constructor.sourceforge.net

As a relatively new user of wxPython, and a non-user of wxWindows, I'm
not sure how much my vote counts for, but I find your example below
clearer/cleaner with properties. Since one of the selling points of
Python is clarity and a less steep learning curve, it makes sense to add
to the clarity, even if it costs a bit.

···

Robin Dunn wrote:
>
> I've been experimenting with adding properties to wxPython.
> (Properties are attributes that magically turn into Get or Set
> method calls.) They let you do things like
>
> print window.size
> window.size = (300,200)
>
> instead of
>
> print window.GetSize()
> window.SetSize( (300,200) )

--
Kevin Cole, Linux Admin | E-mail: kjcole@gri.gallaudet.edu
Gallaudet Research Institute | WWW: http://gri.gallaudet.edu/
Hall Memorial Bldg S-419 | Voice: (202) 651-5135
Washington, D.C. 20002-3695 | FAX: (202) 651-5746

I think what's most important is stability. The name clashes worry me,
unless you can avoid it somehow, like with a prefix. Besides, GetSize,
SetSize, etc. are not difficult.
Bob

···

At 11:55 PM 6/28/00 -0700, you wrote:

I've been experimenting with adding properties to wxPython. (Properties are
attributes that magically turn into Get or Set method calls.) They let you
do things like

   print window.size
   window.size = (300,200)

instead of

   print window.GetSize()
   window.SetSize( (300,200) )

It acutally turned out to be a bit easier than I was expecting. It is even
possible for classes implemented in Python to add their own properties or
override those in the base classes.

A potential problem is name clashes. (The demo has a few already.) It can
be a bit suprizing sometimes that assigning something to self.sizer doesn't
actually save a local reference to the something but calls a method instead.
Especially if it worked fine yesterday before the properties and acts
wierdly today...

So what's your opinion on properties? Is the syntactic sugar worth the
potential hassles? Should it wait until there is Python specific
documentation so you don't have to search the sources to know what names are
magic?

FYI, here are the properties for wxWindow and the methods that would be
called to access or set them:

   _prop_list_ = {
       'size' : ('GetSize', 'SetSize'),
       'enabled' : ('IsEnabled', 'Enable'),
       'background' : ('GetBackgroundColour',
'SetBackgroundColour'),
       'foreground' : ('GetForegroundColour',
'SetForegroundColour'),
       'children' : ('GetChildren', None),
       'charHeight' : ('GetCharHeight', None),
       'charWidth' : ('GetCharWidth', None),
       'clientSize' : ('GetClientSize', 'SetClientSize'),
       'font' : ('GetFont', 'SetFont'),
       'grandParent' : ('GetGrandParent', None),
       'handle' : ('GetHandle', None),
       'label' : ('GetLabel', 'SetLabel'),
       'name' : ('GetName', 'SetName'),
       'parent' : ('GetParent', None),
       'position' : ('GetPosition', 'SetPosition'),
       'title' : ('GetTitle', 'SetTitle'),
       'style' : ('GetWindowStyleFlag',
'SetWindowStyleFlag'),
       'visible' : ('IsShown', 'Show'),
       'shown' : ('IsShown', 'Show'),
       'toolTip' : ('GetToolTip', 'SetToolTip'),
       'sizer' : ('GetSizer', 'SetSizer'),
       'validator' : ('GetValidator', 'SetValidator'),
       'dropTarget' : ('GetDropTarget', 'SetDropTarget'),
       'caret' : ('GetCaret', 'SetCaret'),
       'autoLayout' : ('GetAutoLayout', 'SetAutoLayout'),
       'constraints' : ('GetConstraints', 'SetConstraints'),
   }

--
Robin Dunn
Software Craftsman
robin@AllDunn.com
http://wxpython.org Java give you jitters?
http://wxpros.com Relax with wxPython!

_______________________________________________
wxPython-users mailing list wxPython-users@wxwindows.org
http://wxwindows.org/mailman/listinfo/wxpython-users

-------------------------------------------------
Robert B. Klimek
NASA Glenn Research Center
robert.klimek@grc.nasa.gov
(216) 433-2837
--------------------------------------------------

Robin,

Put me down for one vote for properties. Also, one vote for property names
with leading lowercase characters; the capitalized method names in
wxWindows make me crazy, since they are inconsistant with nearly all other
python libraries (not to mention common practice in e.g. Java where
preferred usage is to capitalize class names to distinguish them from
attributes). Using properties clarifies the programmer's intent and is
more consistent with languages that many new python users might have used
prior to achieving enlightenment (e.g. VB). It will ease the migration to
this terrific tool for a good many folks.

I disagree with those who suggest that they don't like properties that hide
methods. When methods and member data are all treated with similar syntax,
it is much easier to add functionality without breaking application code.
Data hiding is always beneficial, and often code hiding is as well; I don't
think it's necessarily a good idea to distinguish between the two.

I understand that properties are a piece of syntactic sugar that hides the
the "traditional" accoessor functions -- and I like sweet things. C++
doesn't let you make this type of candy; you need to use accessor functions
because properties don't exist. C++ limitations need not be forced upon
wxPython.

Go for it!

/v...

···

> Robin Dunn wrote:
> >
> > I've been experimenting with adding properties to wxPython.
> > (Properties are attributes that magically turn into Get or Set
> > method calls.) They let you do things like
> >
> > print window.size
> > window.size = (300,200)
> >
> > instead of
> >
> > print window.GetSize()
> > window.SetSize( (300,200) )

--
Kevin Cole, Linux Admin | E-mail: kjcole@gri.gallaudet.edu
Gallaudet Research Institute | WWW: http://gri.gallaudet.edu/
Hall Memorial Bldg S-419 | Voice: (202) 651-5135
Washington, D.C. 20002-3695 | FAX: (202) 651-5746

_______________________________________________
wxPython-users mailing list wxPython-users@wxwindows.org
http://wxwindows.org/mailman/listinfo/wxpython-users

Speaking as a pro-props party member :slight_smile:

Vic Kelson wrote:

Robin,

Put me down for one vote for properties. Also, one vote for property names
with leading lowercase characters; the capitalized method names in
wxWindows make me crazy, since they are inconsistant with nearly all other

Well capitals in wxWindows aren't going to go away so it might be
better to stay consistent within the wxWindows framework.
I'd still prefer capitals to a prefix.

If anybody is interested in using properties in their own programs,
I included a file called properties.py in the 0.0.3 release of Boa.

This module allows you to derive from a class where the derived class
will for example when accessing 'size' and the attribute is not found
look if get_size is defined and call that, else if _size is
defined, set that, same for getters.

···

--
Riaan >>> a='a=%s;a%%`a`';a%`a`
___________________________________________________
Boa Constructor - RAD GUI building IDE for wxPython
     http://boa-constructor.sourceforge.net