Embedding Python and built-in types
Jeff Epler
jepler at unpythonic.net
Fri Nov 29 16:16:50 EST 2002
On Fri, Nov 29, 2002 at 10:04:11PM +0100, Mickael Putters wrote:
> Well, I was talking about the assignment operator, maybe it's not =.
> Yeah well guess I'll have to modify the source, I thought maybe there would
> be a way to do that already present in Python/C.
> Anyway, thanks.
There is no "assignment operator for integers". The code to execute
x = v
is the same no matter the type of v (or the type x may have had before the
assignment, as long as you ignore the possible destructor call on decref).
(In C, for a function local variable x, it's basically a decrement of the
reference count of the old local, an increment of the reference count of
'v', and a store of the 'v' in the locals array) "Everything is a
reference"
If you want to override assignments like
o.x = v
then you can override __setattr__ for the object o. Similarly,
there are __setitem__ and __setslice__. In 2.2 and newer, when you
subclass object you can use property() to do the same thing as __setattr__
in a nicer way.
Jeff
More information about the Python-list
mailing list