473,466 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

forcing a definition to be called on attribute change

Hey All,
Apologies if this is a stupidly obvious or simple question. If I have a
class with a series of attributes, is there a way to run a function
definition in the class whenever a specific attribute is changed?

Something like the following

class cSphere() :

def __init__(self):
Self.myAttr = 0

def runThisFunctionWhenMyAttrChanged() :
Pass


Thanks

Mike.

Oct 24 '06 #1
4 1008
Michael Malinowski wrote:
Hey All,
Apologies if this is a stupidly obvious or simple question. If I have a
class with a series of attributes, is there a way to run a function
definition in the class
s/run a function definition in the class/call a method/
whenever a specific attribute is changed?
Yes : properties.
Something like the following

class cSphere() :
OT : prefixing classes names with 'c' is totally unpythonic.
def __init__(self):
Self.myAttr = 0
s/Self/self/
def runThisFunctionWhenMyAttrChanged() :
Pass

class MyClass(object):
def __init__(self):
self.my_attr = 0 # will call the setter
# use direct attribute access instead if you don't
# want to call the setter, ie:
# self._my_attr = 0

def _get_my_attr(self):
return self._my_attr

def _set_my_attr(self, val):
self._my_attr = val
self.runThisFunctionWhenMyAttrChanged()

my_attr = property(_get_my_attr, _set_my_attr)

HTH

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 24 '06 #2
Bruno Desthuilliers wrote in news:45***********************@news.free.fr in
comp.lang.python:
>class cSphere() :

OT : prefixing classes names with 'c' is totally unpythonic.
My understanding of "pythonic" is that its about how you use the
language not what style you code in.

Here's a variation of the usual example of pythonic:

use:

for i in some_list:
pass

not:

i = 0
while i < len( some_list ):
i += 1

IOW, pythonic code is code that uses the features of the language
for the purpose they are intended, not code that ignores python's
features and tries to adapt features or idiom's from other languages.

I guess we could claim that one-liners are unpythonic (because
indentation is a language feature), but claiming a all PEP 8
violating styles are unpyhonic seems to me to be devaluing the
term.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Oct 24 '06 #3
In <Xn**********************************@216.196.109. 145>, Rob Williscroft
wrote:
Bruno Desthuilliers wrote in news:45***********************@news.free.fr in
comp.lang.python:
>>class cSphere() :

OT : prefixing classes names with 'c' is totally unpythonic.

My understanding of "pythonic" is that its about how you use the
language not what style you code in.

[…]

I guess we could claim that one-liners are unpythonic (because
indentation is a language feature), but claiming a all PEP 8
violating styles are unpyhonic seems to me to be devaluing the
term.
IMHO "pythonic" is about readability and sticking to a naming convention
helps others to read your code. If something is called `Sphere` you know
it's a class there's no need to tack a ``c`` in front.

We've seen this just for the class name here but prefixing names with
"type information" is "unpythonic" too, again IMHO, because this can get
very misleading if you change the type. Then you have to change all
occurrences of the name or end up with names like `lstObj` bound to a
set object or something similar.

Ciao,
Marc 'BlackJack' Rintsch
Oct 24 '06 #4
Rob Williscroft a écrit :
Bruno Desthuilliers wrote in news:45***********************@news.free.fr in
comp.lang.python:

>>>class cSphere() :

OT : prefixing classes names with 'c' is totally unpythonic.


My understanding of "pythonic" is that its about how you use the
language not what style you code in.
It's both idioms and style.
Oct 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
6
by: insane79 | last post by:
Hi, i have one problem with an xsl trasformation (I'm using Xalan) The source XML document is the following: <?xml version = '1.0' encoding = 'ISO-8859-1'?> <ROOT...
1
by: Ralph Snart | last post by:
i'm trying to do the classic alternating table row colors trick. i can't use position() mod 2 in this case because sometimes the template is called but it decides not to print the particular...
2
by: petev | last post by:
Hello, can anyone help me out?? Maybe its a bit complicated, maybe not :) I'll try to explain. Am not a programmer so have patience! I have a website whose url looks like this:...
9
by: Curious Student | last post by:
Some places till now, I've seen function prototypes within functions instead of in the global declaration space, which I thought was the way. I thought it was <I>only</I>: int myfunction(int,...
8
by: reviswami78 | last post by:
Hi : I wanted to know if the following is possible in C# ( .NET Framework 1.x): I want to add an attribute to a class in a separate file i.e. in a file other than which the class itself is...
13
by: Lad | last post by:
If I have a list Mylist= I can print it for i in Mylist: print i and results is 1
3
by: Ken Fine | last post by:
I am using ASP.NET's CreateUserWizard control. I want to force the visitor to use a username and e-mail address that I am providing in programming, and I do not want the visitor to be able to edit...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.