Using equals operator without changing reference pointer

Terry Reedy tjreedy at udel.edu
Thu Jan 29 23:03:45 EST 2009


Erik Max Francis wrote:
> mark.seagoe at gmail.com wrote:
> 
>> Is there a way to lock down myInst so that it still refers to the
>> original object, and is there some special member that will allow me
>> to override the equals operator in this case?  Or is that simply
>> blasphemous against everything Python holds sacred?  Certainly there
>> is some keyword that I don't know about.
> 
> No.  The assignment operator with a bare name on the left hand side is 
> not overridable.

So that 'name = ob' *always* binds name to ob.  That is one thing one 
can depend on when reading Python code.

> You can override attribute access, however, with 
> .__getattr__/.__getattribute__.

I presume that you have over-riden __setitem__ in addition to 
__getitem__ so that myOb[0] = 1 sets the bit. You could add a branch to 
__setitem__ (or define __setslice__ in 2.x) so that myOb[:] = 0x55 does 
just what you want it to -- set all bits.  Being able to get/set 
contiguous bits might be something you want anyway.

tjr

PS. When asking about internal details, specify version of interest, as 
there have been minor changes.





More information about the Python-list mailing list