Fire event when variable is Set/Get

  • Thread starter =?ISO-8859-1?Q?Varghj=E4rta?=
  • Start date
?

=?ISO-8859-1?Q?Varghj=E4rta?=

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.
 
T

tharaka

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

=?ISO-8859-1?Q?Varghj=E4rta?=

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.
 
B

Benji York

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

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

Forum statistics

Threads
474,262
Messages
2,571,310
Members
47,977
Latest member
MillaDowdy

Latest Threads

Top