Using equals operator without changing reference pointer

M

mark.seagoe

I know this may be asking too much of Python, but you never know
unless you ask...

So now having a class that can be treated like an int as far as math
upon the instance name, and can be treated as in HDL simulators,
allowing bitslice and bitselect ops, I was stoked. Also reading back
the instance name returns the int, rather than text about the class
name and location in memory. But... it would be wonderful if there
was a way to assign a value without using any .member notation... if
there were a way for me to specify that the reference would never
change. For example:
myInst = MyClass(0xAA)
myInst 170
myInst[0] # <== MyClass can do bit selection 0
myInst = 0x55 # <== gets reassigned
myInst[0] # <== this is where it would choke
1 # <== this is the answer I would want

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.

Thanks!
Mark
 
T

Terry Reedy

Erik said:
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.
 
M

mark.seagoe

Erik said:
(e-mail address removed) wrote:
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.

OK. Thanks for your advice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top