If you feel that the global directive don't fit Python language. Here is a short class for your aesthetic sensibility.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class globaliser:
def __getattr__(self,name):
return globals()[name]
def __setattr__(self,name,value):
globals()[name]=value
glob = globaliser()
def test1():
glob.data = 'hello'
def test2():
print data
print glob.data
test1()
test2()
|
I imagine that the global directive have been introduced in Python before classes. But I find quite unconsistent to use directive for globals and scope qualifier (self) for obects attributes.
The globaliser class provide a scope qualifier for globals.
Tags: oop