473,287 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Descriptors and side effects

Hello everyone,

I'm trying to do seemingly trivial thing with descriptors: have
another attribute updated on dot access in object defined using
descriptors.

For example, let's take a simple example where you set an attribute s
to a string and have another attribute l set automatically to its
length.
>>class Desc(str):
def __init__(self,val):
self.s=val
self.l=len(val)
print "creating value: ", self.s
print "id(self.l)", id(self.l)
def __set__(self, obj, val):
self.s=val
self.l=len(val)
print "setting value:", self.s, "length:", self.l
def __get__(self, obj, type=None):
print "getting value:", self.s, "length:", self.l
return self.l

>>class some(str):
m=Desc('abc')
l=m.l
creating value: abc
id(self.l) 10049688
>>ta=some()
ta.m='test string'
setting value: test string length: 11

However, the attribute ta.l didn't get updated:
>>ta.l
3

This is so much weirder that object id of ta.l is the same as id of
instance of descriptor:
>>id(ta.l)
10049688

A setter function should have updated self.l just like it updated
self.s:

def __set__(self, obj, val):
self.s=val
self.l=len(val)
print "setting value:", self.s, "length:", self.l

Yet it didn't happen.
>From my POV, the main benefit of a descriptor lies in its side effect:
on dot access (getting/setting) I can get other attributes updated
automatically: say, in class of Squares I get area automatically
updated on updating side, etc.

Yet, I'm struggling with getting it done in Python. Descriptors are a
great idea, but I would like to see them implemented in Python in a
way that makes it easier to get desireable side effects.

Nov 5 '07 #1
7 1333
mr****@gmail.com a écrit :
Hello everyone,

I'm trying to do seemingly trivial thing with descriptors: have
another attribute updated on dot access in object defined using
descriptors.

For example, let's take a simple example where you set an attribute s
to a string and have another attribute l set automatically to its
length.
>>>class Desc(str):
def __init__(self,val):
self.s=val
self.l=len(val)
print "creating value: ", self.s
print "id(self.l)", id(self.l)
def __set__(self, obj, val):
self.s=val
self.l=len(val)
print "setting value:", self.s, "length:", self.l
def __get__(self, obj, type=None):
print "getting value:", self.s, "length:", self.l
return self.l

>>>class some(str):
m=Desc('abc')
l=m.l
First point : I don't get why Desc and some derive from str. Second
point: I don't get why you're storing the value and it's length in the
descriptor itself - obviously, you can't expect one descriptor instance
to be mapped to 2 distinct attributes. Third point: you understand that,
the way you wrote it, your descriptor will behave as a class (ie:shared)
attribute, don't you. Fourth point: if you hope some.l to be rebound
when m.l is, then you should learn Python basics before trying to jump
into descriptors.

The obvious, simple way to some your problem is to use a couple of
properties:

class Some(object):
@apply
def m():
def fget(self):
return self._m
def fset(self, val):
self._m = val
self._l = len(val)
return property(**locals())
@apply
def l():
def fget(self):
return self._l
def fset(self):
raise AttributeError("%s.l is readonly" % self)
def __init__(self, m):
self.m = m

Now if you absolutely insist on using custom descriptors, you'll need
two of them: one to manage access to s, and the second to manage access
to l (which btw is a very bad name):

class DescS(object):
def __init__(self, val):
self._default = val

def __set___(self, obj, val):
obj._s = val
obj._l = len(val)

def __get__(self, obj, cls):
if obj is None:
return self # or self._default, or whatever
try:
return obj._s
except AttributeError:
return self._default
class DescL(object):
def __init__(self, descS):
self._descS = descS
def __get__(self, obj, cls):
if obj is None:
return self # or self._default, or whatever
try:
return obj._l
except AttributeError:
return len(self._descS._default)
class Test(object):
m = DescS('abc')
l = DescL(m)

(nb : not tested)
Nov 5 '07 #2
mr****@gmail.com wrote:
Hello everyone,

I'm trying to do seemingly trivial thing with descriptors: have
another attribute updated on dot access in object defined using
descriptors.
[snip]
A setter function should have updated self.l just like it updated
self.s:

def __set__(self, obj, val):
self.s=val
self.l=len(val)
print "setting value:", self.s, "length:", self.l

Yet it didn't happen.
[snip]

I noticed that Python will block all attribute overrides (either via
__dict__ through setattr) if the property has a __set__ method. The
standard property has this method and there is no way that I can find to
defeat it. So, here is what I use:

class ConstProperty(object):
"""
Provides a property that keeps its return value. The function will
only be called on the first access. After that the same value can
be used over and over again with no function call penalty. If the
cached value needs to be cleared, simply del the attribute.
>>class MyClass(object):
... def __init__(self, x):
... self.x = x
... @ConstProperty
... def y(self):
... print "HERE"
... return self.x ** 2
...
>>obj = MyClass(5)
obj.y
HERE
25
>>obj.y
25
"""

def __init__(self, fn):
self.fn = fn

def __get__(self, target, cls=None):
if target is None:
return self.fn # Helps pydoc
else:
obj = self.fn(target)
setattr(target, self.fn.__name__, obj)
return obj

This is a little different than what you originally posted, but
hopefully it is close enough to be helpful.

Cheers!
Rich

Nov 5 '07 #3
Rich Harkins a écrit :
mr****@gmail.com wrote:
>Hello everyone,

I'm trying to do seemingly trivial thing with descriptors: have
another attribute updated on dot access in object defined using
descriptors.

[snip]
>A setter function should have updated self.l just like it updated
self.s:

def __set__(self, obj, val):
self.s=val
self.l=len(val)
print "setting value:", self.s, "length:", self.l

Yet it didn't happen.
[snip]

I noticed that Python will block all attribute overrides (either via
__dict__ through setattr) if the property has a __set__ method.
It doesn't "block", it controls access to... Of course, if the __set__
method is a no-op, then nothing will happen.
The
standard property has this method and there is no way that I can find to
defeat it.
"defeat" ? Why don't you just pass the appropriate fset function to
property ?
So, here is what I use:

class ConstProperty(object):
"""
Provides a property that keeps its return value. The function will
only be called on the first access. After that the same value can
be used over and over again with no function call penalty. If the
cached value needs to be cleared, simply del the attribute.
>>class MyClass(object):
... def __init__(self, x):
... self.x = x
... @ConstProperty
... def y(self):
... print "HERE"
... return self.x ** 2
...
>>obj = MyClass(5)
>>obj.y
HERE
25
>>obj.y
25
"""

def __init__(self, fn):
self.fn = fn

def __get__(self, target, cls=None):
if target is None:
return self.fn # Helps pydoc
else:
obj = self.fn(target)
setattr(target, self.fn.__name__, obj)
return obj

>>m = MyClass(5)
m.__dict__
{'x': 5}
>>m.y
HERE
25
>>m.__dict__
{'y': 25, 'x': 5}
>>m.x = 42
m.y
25
>>m.__dict__
{'y': 25, 'x': 42}
>>>

I'm sorry, but this looks like a very complicated way to do a simple thing:

class MySimpleClass(object):
def __init__(self, x):
self.x = x
self.y = x ** 2
Nov 5 '07 #4
Bruno Desthuilliers wrote:
[snip]
I'm sorry, but this looks like a very complicated way to do a simple thing:

class MySimpleClass(object):
def __init__(self, x):
self.x = x
self.y = x ** 2

Sure, for the absurdly simplified case I posed as an example. ;)

Here's another:

class Path(tuple):
@ConstProperty
def pathstr(self):
print "DEBUG: Generating string"
return '/'.join(self)

def __add__(self, other):
if isinstance(other, tuple):
return Path(tuple.__add__(self, other))
else:
return Path(tuple.__add__(self, (other,)))
>>ROOT = Path(())
path = ROOT + 'x' + 'y' + 'z'
path.pathstr
DEBUG: Generating string
/x/y/z
>>path.pathstr
/x/y/z

Basically, you can use ConstProperty above for items you don't want to
calculate automatically, but only when someone actually WANTS it. After
it is applied, then the penalties for function call of the property and
the computation are wiped out once the second access is requested.

Now, in the original example, len() might be considered too little for
this use and should be just generated in the constructor "for free".
OTOH, that assumes that __len__ hasn't been overridden to do something
more complicated and time consuming. If the antecedent object is
static, and the derivative consequent is also static, then ConstProperty
works very well and shouldn't cost more on the first access than any
other built-in property function.

BTW, another use is to avoid creating lots of unnecessary objects for
free unless they are accessed. Another quickie example:

class Node(object):
hasChildList = False
hasAttributesDict = False

@ConstProperty
def children(self):
self.hasChildList = True
return []

@ConstProperty
def attributes(self):
self.hasAttributesDict = True
return {}

The extra class/object attributes can be used to test for whether the
associated objects were created. When used in a large tree, not
creating a lot of extra lists and dictionaries can save a lot of memory
and CPU as the children and attributes are not created or explored
unless they were manipulated.

Rich
Nov 5 '07 #5
Bruno Desthuilliers wrote:
Rich Harkins a écrit :
>mr****@gmail.com wrote:
>>Hello everyone,

I'm trying to do seemingly trivial thing with descriptors: have
another attribute updated on dot access in object defined using
descriptors.
[snip]
>>A setter function should have updated self.l just like it updated
self.s:

def __set__(self, obj, val):
self.s=val
self.l=len(val)
print "setting value:", self.s, "length:", self.l

Yet it didn't happen.
[snip]

I noticed that Python will block all attribute overrides (either via
__dict__ through setattr) if the property has a __set__ method.

It doesn't "block", it controls access to... Of course, if the __set__
method is a no-op, then nothing will happen.
>The
standard property has this method and there is no way that I can find to
defeat it.

"defeat" ? Why don't you just pass the appropriate fset function to
property ?
> So, here is what I use:

class ConstProperty(object):
"""
Provides a property that keeps its return value. The function will
only be called on the first access. After that the same value can
be used over and over again with no function call penalty. If the
cached value needs to be cleared, simply del the attribute.
> >>class MyClass(object):
... def __init__(self, x):
... self.x = x
... @ConstProperty
... def y(self):
... print "HERE"
... return self.x ** 2
...
> >>obj = MyClass(5)
>>obj.y
HERE
25
> >>obj.y
25
"""

def __init__(self, fn):
self.fn = fn

def __get__(self, target, cls=None):
if target is None:
return self.fn # Helps pydoc
else:
obj = self.fn(target)
setattr(target, self.fn.__name__, obj)
return obj


>>m = MyClass(5)
>>m.__dict__
{'x': 5}
>>m.y
HERE
25
>>m.__dict__
{'y': 25, 'x': 5}
>>m.x = 42
>>m.y
25
>>m.__dict__
{'y': 25, 'x': 42}
>>>


I'm sorry, but this looks like a very complicated way to do a simple thing:

class MySimpleClass(object):
def __init__(self, x):
self.x = x
self.y = x ** 2

Nov 5 '07 #6
Rich Harkins a écrit :
Bruno Desthuilliers wrote:
[snip]
>>I'm sorry, but this looks like a very complicated way to do a simple thing:

class MySimpleClass(object):
def __init__(self, x):
self.x = x
self.y = x ** 2



Sure, for the absurdly simplified case I posed as an example. ;)

Here's another:

class Path(tuple):
Ok, for an immutable type, it might eventually work.
@ConstProperty
def pathstr(self):
print "DEBUG: Generating string"
return '/'.join(self)
import os.path
help(os.path)
def __add__(self, other):
if isinstance(other, tuple):
return Path(tuple.__add__(self, other))
else:
return Path(tuple.__add__(self, (other,)))

>>>>ROOT = Path(())
path = ROOT + 'x' + 'y' + 'z'
path.pathstr

DEBUG: Generating string
/x/y/z
>>>>path.pathstr

/x/y/z
>>p = Path(('home', 'bruno'))
p += ['toto', 'tata']
p.pathstr
DEBUG: Generating string
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/tmp/python-8690chu", line 31, in __get__
File "/usr/tmp/python-8690chu", line 40, in pathstr
TypeError: sequence item 2: expected string, list found
>>>
Basically, you can use ConstProperty above for items you don't want to
calculate automatically, but only when someone actually WANTS it.
Which is easy to do with properties too.
After
it is applied, then the penalties for function call of the property and
the computation are wiped out once the second access is requested.
Agreed. But I wouldn't use such a scheme for mutable types - which are
still the common case.
Now, in the original example, len() might be considered too little for
this use and should be just generated in the constructor "for free".
OTOH, that assumes that __len__ hasn't been overridden to do something
more complicated and time consuming. If the antecedent object is
static, and the derivative consequent is also static,
You mean 'immutable', I assume...
then ConstProperty
works very well and shouldn't cost more on the first access than any
other built-in property function.

BTW, another use is to avoid creating lots of unnecessary objects for
free unless they are accessed. Another quickie example:

class Node(object):
hasChildList = False
hasAttributesDict = False

@ConstProperty
def children(self):
self.hasChildList = True
return []

@ConstProperty
def attributes(self):
self.hasAttributesDict = True
return {}
Hmm... Perhaps not such a bad idea after all !-)
Nov 5 '07 #7
Bruno Desthuilliers wrote:
Which is easy to do with properties too.
True enough. It's the caching of the return value that's the value add
of course. ;)
>
> After
it is applied, then the penalties for function call of the property and
the computation are wiped out once the second access is requested.

Agreed. But I wouldn't use such a scheme for mutable types - which are
still the common case.
In many cases, yeah. Though I use a lot of immutable stuff in some of
my pet projects and such. ConstProperty is definitely not meant as a
replacement for property, only when something constant can be derived
from something else constant, especially when the derivation is expensive.
>Now, in the original example, len() might be considered too little for
this use and should be just generated in the constructor "for free".
OTOH, that assumes that __len__ hasn't been overridden to do something
more complicated and time consuming. If the antecedent object is
static, and the derivative consequent is also static,

You mean 'immutable', I assume...
Yeah, that's probably the better term.

[snip]

Again, I've used it quite a bit for various things and it's worked well
for the sort of thing the OP was requesting. Of course, your mileage
may vary. :)

Cheers!
Rich

PS: Sorry about the weird reposts. Thunderbird chaos.

Nov 5 '07 #8

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

Similar topics

3
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: ...
13
by: Steve Jorgensen | last post by:
== On Error Resume next, and Err.Number == If you want to call one of your procedures from another procedure, and check for errors afterward, you mayimagine that you should write code something...
23
by: Mantorok Redgormor | last post by:
Can emulation of the logical OR be done in standard C to obfuscate its use? So I don't have to use if(a||b) but instead make that even more obfuscated without its use but testing the same condition
9
by: Rouben Rostamian | last post by:
Consider the following illustrative program: #include <stdio.h> double f(double x) { return x*x; } double g(double x)
13
by: Mantorok Redgormor | last post by:
does volatile really inhibit side effects? that is the rules for sequence points and side effects do not apply to volatile objects? -- nethlek
5
by: Niklaus | last post by:
This is one of the posts that i got. ------------------------------ A "side effect" of an operation is something that *happens*, not something that *is produced*. Examples: In the expression...
17
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce...
6
by: Senthil | last post by:
Hi, Whenever i read a C++ book or a newsgroup posting, i come across the terms like " before the side effects completes" , "destructor with side effects" etc. What is this side effect mean in C++...
4
by: Academia | last post by:
I get the following watch message: tsSource.Text.ToUpper() This expression causes side effects and will not be evaluated string The Text is &Edit
2
isladogs
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...
0
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
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 =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
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...
1
isladogs
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.