Question about a single underscore.

Steven W. Orr steveo at syslang.net
Thu Feb 1 16:06:20 EST 2007


On Thursday, Feb 1st 2007 at 21:45 +0100, quoth Bruno Desthuilliers:

=>Steven W. Orr a écrit :
=>> I saw this and tried to use it:
=>> 
=>> ------------------><8------------------- 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
=>> 
=>> import sys
=>> sys.modules[__name__]=_const()
=>> ------------------><8------------------- const.py-------------
=>> 
=>> Then when I go to try to use it I'm supposed to say:
=>> 
=>> const.pi        = 3.14159
=>> const.e         = 2.7178
=>> 
=>> 
=>> Two questions:
=>> 
=>> 1. Why do I not have to say
=>> 
=>> _const.pi        = 3.14159
=>> _const.e         = 2.7178
=>
=>Because of the last two lines of module const. sys.modules is a dict of 
=>already imported modules (yes, Python's modules are objects too), which 
=>avoids modules being imported twice or more. __name__ is a magic 
=>variable that is set either to the name of the module - when it's 
=>imported - or to '__main__' - when it's being directly executed as the 
=>main program. Here, when you first do 'import const', the module's code 
=>itself sets sys.modules['const'] to an instance of _const, so what you 
=>import in your namespace as 'const' is not the const module instance but 
=>a _const instance.
=>
=>> and is this in the tutorial?
=>
=>Hmmm... Not sure. FWIW, it's mostly a hack !-)
=>
=>> 2. Can I make this behave in such a way that I can create my constants 
=>> with a classname
=>
=>s/classname/name/
=>
=>> that is different for different uses? e.g.,
=>> 
=>> irrational_const.pi        = 3.14159
=>> irrational_const.e         = 2.7178
=>> 
=>> even_const.first    = 2
=>> even_const.firstPlus    = 4
=>
=>irrational_const = const.__class__()
=>even_const = const.__class__()
=>
=>Now while I find this hack interesting, it's also totally unpythonic 
=>IMHO. The usual convention is to use ALL_UPPER names for (pseudo) 
=>symbolic constants, and I don't see any reason to forcefit B&D languages 
=>concepts into Python.

Ok. Now *maybe* we're getting to where I want to be. My const.py now looks 
like this:

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()

and in a seperate module call key_func_consts I say:

import const
# Define constants for Key Function Field.
KeyFuncConst   = const.__class__()
KeyFuncConst.NotDefined                = 0x00

And in my main module I have
#! /usr/bin/python
import const
import key_func_consts
print KeyFuncConst.MSK


but the last print fails with 

The goal is to be able to create classes of global constants.

561 > t_const.py 
Traceback (most recent call last):
  File "./t_const.py", line 6, in ?
    print KeyFuncConst.MSK
NameError: name 'KeyFuncConst' is not defined

Do I need to get the KeyFuncConst object into sys.modules somehow? I know 
I'm close.

-- 
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