pep 8 constants

Robin Becker robin at reportlab.com
Wed Feb 25 05:07:24 EST 2009


well this sort of awful hackery will allow you to put read only constants on an 
existing module

 >>> import reportlab
 >>> reportlab.__class__
 >>> class MyModule(reportlab.__class__):
... 	@property
... 	def pi(self):
... 		return 3
...
 >>> z=MyModule('reportlab')
 >>> z.__dict__.update(reportlab.__dict__)
 >>> z.pi
3
 >>> import sys
 >>> sys.modules['reportlab']=z
 >>> del reportlab
 >>> import reportlab
 >>> reportlab.pi
3
 >>> reportlab.pi=4
Traceback (most recent call last):
   File "<interactive input>", line 1, in <module>
AttributeError: can't set attribute
 >>>

so I guess if you write your own module class and then use a special importer 
you can create module like objects with read only attributes.


-- 
Robin Becker




More information about the Python-list mailing list