Question about a single underscore.

Steven W. Orr steveo at syslang.net
Thu Feb 1 13:15:19 EST 2007


On Thursday, Feb 1st 2007 at 09:25 -0800, quoth Bart Ogryczak:

=>On Feb 1, 5:52 pm, "Steven W. Orr" <ste... at syslang.net> wrote:
=>> I saw this and tried to use it:
=>>
=>> ------------------><8------------------- const.py-------------
=>[...]
=>> sys.modules[__name__]=_const()
=>
=>__name__ == 'const', so you´re actually doing
=>const = _const()

So how can I say this in const.py?

class _const:
    class ConstError(TypeError): pass
    def __setattr__(self,name,value):
        if self.__dict__.has_key(name):
            raise self.ConstError, "Can't rebind const(%s)"%name
        self.__dict__[name]=value

    def __init__(self):
        sys.modules[self]=_const()

import sys

to cause a different instantiation a la

foo	= _const()

The goal would be to create different instances of consts.

I did try the previous suggestion

irrational_consts = _const()

but I got

>>> import const
>>> iii=_const()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name '_const' is not defined
*>>> iii=const()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: _const instance has no __call__ method

------------
For ref:

class _const:
    class ConstError(TypeError): pass
    def __setattr__(self,name,value):
        if self.__dict__.has_key(name):
            raise self.ConstError, "Can't rebind const(%s)"%name
        self.__dict__[name]=value

import sys
sys.modules[__name__]=_const()



-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net


More information about the Python-list mailing list