Can I reference 1 instance of an object by more names ? rephrase
Peter Otten
__peter__ at web.de
Fri May 25 03:51:57 EDT 2007
Stef Mientki wrote:
>> Again, I'm confident, again I didn't test.
>
> I did, ...
> ... and unfortunately it still gave errors.
Strange.
> So for the moment I'll just stick to my "lots of code solution",
> and I'll try again, when I've some more understanding of these Python
> "internals".
Anyway, here's a self-contained example:
"""
>>> p = cpu_ports()
>>> p
cpu_ports(p1=0, p2=0, p3=0, p4=0, p5=0)
>>> p.p3 = 1
>>> p
cpu_ports(p1=0, p2=0, p3=1, p4=0, p5=0)
>>> p.p4 = 1
>>> p
cpu_ports(p1=0, p2=0, p3=1, p4=1, p5=0)
>>> p.p3 = 0
>>> p
cpu_ports(p1=0, p2=0, p3=0, p4=1, p5=0)
"""
def bit(index):
def fset(self, value):
value = ( value & 1L ) << index
mask = ( 1L ) << index
self._d = ( self._d & ~mask ) | value
def fget(self):
return ( self._d >> index ) & 1
return property(fget, fset)
class cpu_ports(object) :
def __init__(self):
self._d = 0
def __str__(self):
return "cpu_ports(%s)" % ", ".join(
"%s=%s" % (n, getattr(self, n)) for n in ["p1", "p2", "p3", "p4", "p5"])
__repr__ = __str__
p1 = bit(1)
p2 = bit(2)
p3 = bit(3)
p4 = bit(4)
p5 = bit(5)
if __name__ == "__main__":
import doctest
doctest.testmod()
Peter
More information about the Python-list
mailing list