Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fire event when variable is Set/Get

1 view
Skip to first unread message

Varghjärta

unread,
Jul 24, 2005, 11:41:17 PM7/24/05
to pytho...@python.org
Hey!

I'm a hobby programmer since many years now and I've done most of my
latest 'real application' coding in C#. I've played with python of and
on yet not catching on until a few months ago when I got myself hocked
on it for real and now I love C# _and_ Python.

But there is something that keeps bugging me, and that keeps me from
embracing Python even more as a serious alternative to C#(for me). In
C# I make heavy use of Get & Set, mainly to fire an event that some
property has changed so that one can act on that _if one would need
to. I've grown very used to doing it and it feels like the best way to
make very OO and reusuable and easy to use Classes.

Is there _anything_ that I could do in Python which would allow me to
known when a variable has been set or when it's being fetched to allow
me to fire an event there?.

I seriously _hate_ having to use methods for editing variables
(setVariable1("blah")) since it just adds more code -- or perhaps not
but the code doesn't flow as nicely. So I don't count this as a real
valid option.

This might seem like not such a big thing but to me it's such a
problem that it's driving me to even consider trying out Boo (the
c#/python hybrid) when I get the chance eventhough I think i'd rather
use the _original_ (python) now when i'm almost getting the hang of
it.

How does python coders generally deal with this? Does everyone inherit
from a class and override the methods that sets variables and does the
work from there? I love events and use them heavily -- in GUI apps
what else would one use?

Would be thankful for any pointers that would evolve my pythonic strives.

Robert Kern

unread,
Jul 24, 2005, 11:48:34 PM7/24/05
to pytho...@python.org
Varghjärta wrote:
> Hey!
>
> I'm a hobby programmer since many years now and I've done most of my
> latest 'real application' coding in C#. I've played with python of and
> on yet not catching on until a few months ago when I got myself hocked
> on it for real and now I love C# _and_ Python.
>
> But there is something that keeps bugging me, and that keeps me from
> embracing Python even more as a serious alternative to C#(for me). In
> C# I make heavy use of Get & Set, mainly to fire an event that some
> property has changed so that one can act on that _if one would need
> to. I've grown very used to doing it and it feels like the best way to
> make very OO and reusuable and easy to use Classes.
>
> Is there _anything_ that I could do in Python which would allow me to
> known when a variable has been set or when it's being fetched to allow
> me to fire an event there?.

There's Enthought's Traits.

http://code.enthought.com/traits/traits.htm

I don't think it handles the Get part, though. You could probably come
up with something using __getattribute__ for that part.

http://docs.python.org/ref/new-style-attribute-access.html

--
Robert Kern
rk...@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

tharaka

unread,
Jul 25, 2005, 12:06:07 AM7/25/05
to
You are in luck because Python has "Properties" just like .NET.

For details lookup the documentation of the built-in function
property(). I'll just paste it here:


property( [fget[, fset[, fdel[, doc]]]])

Return a property attribute for new-style classes (classes that derive
from object).
fget is a function for getting an attribute value, likewise fset is a
function for setting, and fdel a function for del'ing, an attribute.
Typical use is to define a managed attribute x:


class C(object):
def getx(self): return self.__x
def setx(self, value): self.__x = value
def delx(self): del self.__x
x = property(getx, setx, delx, "I'm the 'x' property.")


... now u can use x like any variable and, python will get & set it
through the appropriate methods. Hope this answers your question.

Mike C. Fletcher

unread,
Jul 25, 2005, 12:22:46 AM7/25/05
to Varghjärta, pytho...@python.org
Varghjärta wrote:
...

>But there is something that keeps bugging me, and that keeps me from
>embracing Python even more as a serious alternative to C#(for me). In
>C# I make heavy use of Get & Set, mainly to fire an event that some
>property has changed so that one can act on that _if one would need
>to. I've grown very used to doing it and it feels like the best way to
>make very OO and reusuable and easy to use Classes.
>
>

It's fairly straightforward to implement observability for any of the
various "modeling" descriptor systems out there. Attached you'll find a
quick demo of how one could use BasicProperty with PyDispatcher to
produce the effect. The biggest problem is that normally the particular
events you want to fire are application specific. Heck, it's a toss-up
whether the property or the client should be the sender of a message
(basically depends on whether you normally want to filter by one or the
other).

The OpenGLContext vrml scenegraph has PyDispatcher-sending fields
built-in if you want to see a real-world use. (We have others which use
BasicProperty + PyDispatcher, but they're not Open-Source).

Have fun,
Mike

--
________________________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

observable.py

gene tani

unread,
Jul 25, 2005, 12:26:52 AM7/25/05
to
this recipe takes medium-deep copies (shallow copies of embedded
sequences/dict's) of an obj's __dict__ when you need to monitor changes
to the object's state from that point on:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302742

Varghjärta

unread,
Jul 25, 2005, 12:37:10 AM7/25/05
to pytho...@python.org
Thank you!

Wow, this might be exactly what I want! Thanks to the pythonness
(syntax) the code might even be shorter then implementing it in C#!

Gonna go and play around with this some more(now), and can't wait til
I get home (there will be some massive code cleaning).

I wonder why I've never come across this before, feels like i've
googled alot these last weeks.

Python has grown in my eyes.

> --
> http://mail.python.org/mailman/listinfo/python-list
>

Benji York

unread,
Jul 25, 2005, 7:36:20 AM7/25/05
to pytho...@python.org
tharaka wrote:
> You are in luck because Python has "Properties" just like .NET.

> class C(object):


> def getx(self): return self.__x
> def setx(self, value): self.__x = value
> def delx(self): del self.__x
> x = property(getx, setx, delx, "I'm the 'x' property.")

Just for those that might have tastes like mine; here's the pattern I've
been using for this (in 2.4, obviously):

class C(object):

@apply
def x():
doc = "I'm the 'x' property."
def fget(self): return self.__x
def fset(self, value): self.__x = value
def fdel(self): del self.__x
return property(**locals())

You can remove any of fget, fset, fdel, or doc without changing any
other lines, and there are no "extra" entries in the class's name space.

--
season-to-taste-ly yours,
Benji York

0 new messages