Hi list:
I have googled quite a bit on this matter and I can't seem to find what
I need (I think Im just looking where I'm not suppose to :). I'm
working with code that is not of my authorship and there is a class
attribute that is changes by directly referencing it (object.attr =
value) instead of using a getter/setter (object.setAttr(Value) )
function. The thing is that I have no idea when the change occurs and I
would REALLY like to find out.
So here comes my question .....
Is there a function construct inside a python class that is
automatically called when an attr is changed????
Like for example
/class Class:
def __init__();
self.attr = "whatever"
def __attrChangeFunction__():
print "It has changed"
obj = Class()
obj.attr = "other whatever"
*Output:*
It has changed
/
Thanks for the help.
Regards/
/ 5 1311
Joel Andres Granados a écrit :
Hi list:
I have googled quite a bit on this matter and I can't seem to find what
I need (I think Im just looking where I'm not suppose to :). I'm
working with code that is not of my authorship and there is a class
attribute that is changes by directly referencing it (object.attr =
value) instead of using a getter/setter (object.setAttr(Value) )
Which is the right thing to do in Python (I mean : *not* using
Java-style getters/setters).
function. The thing is that I have no idea when the change occurs and I
would REALLY like to find out.
So here comes my question .....
Is there a function construct inside a python class that is
automatically called when an attr is changed????
yes : object.__setattr__(self, name, value)
# example:
class Class(object):
def __init__();
self.attr = "whatever"
def __setattr__(self, name, value):
object.__setattr__(self, name, value)
if name == 'attr':
print "It has changed"
# you can also print the call frame,
# or set a 'hard' breakpoint here...
obj = Class()
obj.attr = "other whatever"
*Output:*
It has changed
Or you might turn attr into a property:
class Class(object):
def __init__();
self.attr = "whatever"
@apply
def attr():
def fset(self, value):
self._attr = value
print "It has changed"
def fget(self):
return self._attr
return property(**locals())
But unless you have other needs than simple tracing/debugging, it's
probably overkill.
HTH
Joel Andres Granados a écrit :
Hi list:
I have googled quite a bit on this matter and I can't seem to find what
I need (I think Im just looking where I'm not suppose to :). I'm
working with code that is not of my authorship and there is a class
attribute that is changes by directly referencing it (object.attr =
value) instead of using a getter/setter (object.setAttr(Value) )
function. The thing is that I have no idea when the change occurs and I
would REALLY like to find out.
So here comes my question .....
Is there a function construct inside a python class that is
automatically called when an attr is changed????
Like for example
/class Class:
def __init__();
self.attr = "whatever"
def __attrChangeFunction__():
print "It has changed"
Use a property. Extract from Python 2.3 docs:
"""
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.")
New in version 2.2.
"""
obj = Class()
obj.attr = "other whatever"
*Output:*
It has changed
/
Thanks for the help.
Regards/
/
A+
Laurent.
Bruno Desthuilliers wrote:
Joel Andres Granados a écrit :
>Hi list:
I have googled quite a bit on this matter and I can't seem to find what I need (I think Im just looking where I'm not suppose to :). I'm working with code that is not of my authorship and there is a class attribute that is changes by directly referencing it (object.attr = value) instead of using a getter/setter (object.setAttr(Value) )
Which is the right thing to do in Python (I mean : *not* using
Java-style getters/setters).
>function. The thing is that I have no idea when the change occurs and I would REALLY like to find out. So here comes my question ..... Is there a function construct inside a python class that is automatically called when an attr is changed????
yes : object.__setattr__(self, name, value)
# example:
class Class(object):
def __init__();
self.attr = "whatever"
def __setattr__(self, name, value):
object.__setattr__(self, name, value)
if name == 'attr':
print "It has changed"
# you can also print the call frame,
# or set a 'hard' breakpoint here...
obj = Class()
obj.attr = "other whatever"
*Output:*
It has changed
I used this ^^^^^^^^^^^^^^^ one. Thank for the help.
Works like a charm.
Regards
Or you might turn attr into a property:
class Class(object):
def __init__();
self.attr = "whatever"
@apply
def attr():
def fset(self, value):
self._attr = value
print "It has changed"
def fget(self):
return self._attr
return property(**locals())
But unless you have other needs than simple tracing/debugging, it's
probably overkill.
HTH
En Wed, 14 Mar 2007 10:01:54 -0300, Joel Andres Granados
<jo***********@gmail.comescribió:
Bruno Desthuilliers wrote:
>Joel Andres Granados a écrit :
>>I'm working with code that is not of my authorship and there is a class attribute that is changes by directly referencing it (object.attr = value) instead of using a getter/setter (object.setAttr(Value) )
>yes : object.__setattr__(self, name, value)
I used this ^^^^^^^^^^^^^^^ one. Thank for the help.
The problem with __setattr__ is that it slows down significantly *all*
attributes.
Yours is the typical case when it's good to switch from using simple
attributes to using properties. See
<http://dirtsimple.org/2004/12/python-is-not-java.html>
--
Gabriel Genellina
Gabriel Genellina a écrit :
En Wed, 14 Mar 2007 10:01:54 -0300, Joel Andres Granados
<jo***********@gmail.comescribió:
>Bruno Desthuilliers wrote:
>>Joel Andres Granados a écrit :
>>>I'm working with code that is not of my authorship and there is a class attribute that is changes by directly referencing it (object.attr = value) instead of using a getter/setter (object.setAttr(Value) )
>>yes : object.__setattr__(self, name, value)
I used this ^^^^^^^^^^^^^^^ one. Thank for the help.
The problem with __setattr__ is that it slows down significantly *all*
attributes.
Yours is the typical case when it's good to switch from using simple
attributes to using properties.
If this has to become a feature of the class, yes, indeed. If it's just
a temporary hack for debugging, the using the __getattr__ hook is
quicker and less intrusive. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: svilen |
last post by:
hello again.
i'm now into using python instead of another language(s) for
describing structures of data, including names, structure,
type-checks, conversions, value-validations, metadata etc....
|
by: Randal |
last post by:
Does anyone have a code sample of how one would "listen"
to a TCP/IP session between an application on the local
machine and a remote host.
I'm looking for code that would allow me to specify a...
|
by: johnm |
last post by:
Hello,
We currently are running a CRM application that uses DB/2 7.2 for the data
repository. We will be upgrading to 8.2 later this year....maybe....time and
resources permitting.
The...
|
by: Paul Steele |
last post by:
I am working in a C# program what monitors the activity on a computer. I
have been able to accomplish quite a bit so far, but one item I have not
been able to figure out is if there is a way to...
|
by: JSheble |
last post by:
I have a windows service that in the OnStart it creates a thread and runs a
loop forever and ever, assuming the service is running. The loop stops
during the OnStop event, and everything works...
|
by: Tim D |
last post by:
Hi,
I originally posted this as a reply to a rather old thread in
dotnet.framework.general and didn't get any response. I thought it might be
more relevant here; anyone got any ideas? My...
|
by: developer |
last post by:
I am trying to get a custom provider to receive health monitoring events in
vb.net, whidbey beta 1. I have a health monitoring setup like this in my
web.config:
<healthMonitoring enabled="true">...
|
by: Adam Donahue |
last post by:
As an exercise I'm attempting to write a metaclass that causes an
exception to be thrown whenever a user tries to access
'attributes' (in the traditional sense) via a direct reference.
Consider:...
|
by: Gabriel Rossetti |
last post by:
Hello everyone,
I had read somewhere that it is preferred to use
self.__class__.attribute over ClassName.attribute to access class (aka
static) attributes. I had done this and it seamed to work,...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
| |