Question about a single underscore.

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Feb 1 15:29:23 EST 2007


Paul Rubin a écrit :
> "Steven W. Orr" <steveo at syslang.net> writes:
> 
>>AttributeError: _const instance has no attribute '_const'
>>
>>What am I missing here? (Sorry if it should be obvious)
> 
> 
> Oh I see.  No it's not obvious.  module "const" has gotten overwritten
> by the _const instance.  I think that module author was too clever for
> his or her own good.

Not necessarily - given the intent, it's just smart IMHO. But the 
workaround is quite obvious anyway:

 >>> import const
 >>> # so what is const ?
 >>> const
<const._const instance at 0x40409acc>
 >>> # ok, it's an instance, so it should refer to it's class:
 >>> Const = const.__class__
 >>> Const
<class const._const at 0x403fbdac>
 >>> # Bingo. Let's have another _const instance:
 >>> c = Const()
 >>> c
<const._const instance at 0x404095cc>
 >>> c.pi = 42
 >>> c.pi = 33
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "const.py", line 5, in __setattr__
     raise self.ConstError, "Can't rebind const(%s)"%name
const.ConstError: Can't rebind const(pi)
 >>>

HTH



More information about the Python-list mailing list