forcing a definition to be called on attribute change

M

Michael Malinowski

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

Bruno Desthuilliers

Michael said:
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
 
R

Rob Williscroft

Bruno Desthuilliers wrote in in
comp.lang.python:
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.
 
M

Marc 'BlackJack' Rintsch

Rob Williscroft said:
Bruno Desthuilliers wrote in in
comp.lang.python:
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
 
B

Bruno Desthuilliers

Rob Williscroft a écrit :
Bruno Desthuilliers wrote in in
comp.lang.python:




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.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top