How To capture an id into an array..

Christian Tismer tismer at stackless.com
Mon Mar 29 12:45:46 EST 2004


Balaji wrote:

> Thanks for all the initial help....
> 
> Sorry for bugging you again...
> 
> Here is a part of my code..
> 
> Suppose if some body wants to assign an e=E() to an expression class..
> 
> he may define then e=v+v1(where v and v1 both belong to a variable class)
> 
> now again if we define say v2=V() and then try to multiply v2 with e then I want
> to raise an error that it is a nonlinear term..
> 
> So  what I though was if i keep track of all the instances id then I might raise
> an exception...
> 
> But the problem was i couldnt check the right side of an assigment...
> 
> Can the right side of an assignment be checked...

You can intercept the process of assignment.
In older python, this was done overwriting __setattr__,
but today it is much simpler and cleaner to user property
objects. You can just define a setter that does certain checks.

 >>> print property.__doc__
property(fget=None, fset=None, fdel=None, doc=None) -> property attribute

fget is a function to be used for getting an attribute value, and 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.")
 >>>

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at stackless.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34  home +49 30 802 86 56  mobile +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
      whom do you want to sponsor today?   http://www.stackless.com/





More information about the Python-list mailing list