Controlling assignation

Steven D'Aprano steve at REMOVETHIScyber.com.au
Mon Jun 13 10:44:11 EDT 2005


On Mon, 13 Jun 2005 15:52:14 +0200, Xavier Décoret wrote:

> I would like to know if there is for python's classes an equivalent of 
> the operator= that can be overidden.
> 
> Let's say I have
>  >>> a=A()
> and I want to write
>  >>> a=5
> and I want this to change some internal value of a instead of making a 
> point to a new object (an int 5)
> 
> In other word, I would like to be able to use a=5 instead of a.set(5)
> 
> Is that possible?

I don't know, because I don't understand what you mean.

What is A()? A class or a function? What is a.set()? What does it do?

I'm going to make a wild guess as to what you want to do. You want
something like Pascal records or C structs, right?

py> a = MyCustomShapeClass()
py> print a  # calls a.__repr__ for a nice printable representation
[top = 0, bottom = 1, left = 5, right = 20, kind = "rect"]
py> a.kind = "oval"
py> a.bottom = 100
py> print a
[top = 0, bottom = 100, left = 5, right = 20, kind = "oval"]

Is that the sort of thing you are looking for?


-- 
Steven




More information about the Python-list mailing list