Looking for assignement operator

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Tue Oct 17 10:21:32 EDT 2006


On Tue, 17 Oct 2006 15:50:47 +0200, Alexander Eisenhuth wrote:

> Hello,
> 
> is there a assignement operator, that i can overwrite?

No.

We were just discussing the reasons why Python will not and can not have
an assignment operator just a few days ago. Check the archives for more
details.


> class MyInt:
> 	def __init__(self, val):
> 		assert(isinstance(val, int))

isinstance() considered harmful:

http://www.canonical.org/~kragen/isinstance/

> 		self._val = val

Seems kind of pointless. What does MyInt do that ordinary ints don't?
Apart from slow your program down and require extra programming effort.

> a = MyInt(10)
> 
> # Here i need to overwrite the assignement operator a = 12

Can't happen. "a" is just a name, not an object with methods that can be
called. "a" can be bound to anything, not just MyInt instances. Objects
like MyInt(10) can have no name, one name or many names:

mylist = [0, MyInt(10), 20, 30]  # MyInt instance has no name
x = MyInt(10)  # MyInt instance has one name
x = y = z = MyInt(10)  # MyInt instance has many names



-- 
Steven.




More information about the Python-list mailing list