Can I reference 1 instance of an object by more names ? rephrase

Peter Otten __peter__ at web.de
Thu May 24 18:07:37 EDT 2007


Stef Mientki wrote:

> Peter Otten wrote:
>> Stef Mientki wrote:
>> 
>>> Maric Michaud wrote:
>> 
>>    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(**locals())
>>>>
>>>>
>>>> class cpu_ports(object) :
>> 
>>        p1 = bit(1)
>>        p2 = bit(2)
>>        p3 = bit(3)
>>        p4 = bit(4)
>>        p5 = bit(5)
>> 
>>> Looks good, but I miss the index :-(
>> 
>> No more.
> agreed,
> but Python doesn't like it,
> and I don't understand why

Because the index is now in the locals() dict. Change the return statement
to fix the error:

> def bit(index):
>    def fset(self, value):
>      #index    = 5
>      value    = ( value & 1L ) << index
>      mask     = ( 1L ) << index
>      self._d  = ( self._d & ~mask ) | value
>    def fget(self):
>      #index    = 5
>      return ( self._d >> index ) & 1

     return property(fget, fset)

Again, I'm confident, again I didn't test.

Peter




More information about the Python-list mailing list