Controlling assignation

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Jun 14 19:30:30 EDT 2005


Xavier Décoret a écrit :
> Bruno Desthuilliers a écrit :
> 
(snip)
>> I really wonder what it can be ???
>  
> It's the ability to develop the equivalent of GeoNext (and much more) in 
>  Python with a very nice syntax.

This is nice, but this does not explain the why of your code snippet.

>>
>> You could achieve the same effect with:
>>
>> class A( object):
>>   def __init__(self, value):
>>     self.value = value
>>
>> a = A(3)
>> a.value = 4
>> a.value
>> => 4
>>
> 
> Of course, and even simpler ;-)
> print "4"

of course !-)

> More seriously, try to do this with your simpler approach.
> 
> a = A(4)
> b = A(lambda : a.x+5)
> a.x = 2
> 
> print b.x # I want this to be 7, not 9

I may miss the point, but what about:

class A(object):
   def __init__(self, x):
     self.x = x

   def _getx(self):
     if callable(self._x):
       return self._x()
     else:
       return self._x

   def _setx(self, x):
     self._x = x

   x = property(_getx, _setx)

a = A(4)
b = A(lambda : a.x+5)
a.x = 2
print "b.x is", b.x

<have-fun with="lambdas">
#... and now for the fine part:
del a
b.x

But what, your solution behaves just the same.
</have-fun>


>> Ever googled for "evolution of a programmer" ?-)
>>
> Not to brag, but I think I know what programming is.  

I don't have any reason to doubt it. But do you know what not 
programming is ?-)

> C'est bien un commentaire de francais ca ;-)

<gaston>Meuhnon</gaston>



More information about the Python-list mailing list