Welcome, guest | Sign In | My Account | Store | Cart

If you feel that the global directive don't fit Python language. Here is a short class for your aesthetic sensibility.

Python, 19 lines
 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.

Created by Sébastien Keim on Mon, 8 Oct 2001 (PSF)
Python recipes (4591)
Sébastien Keim's recipes (24)

Required Modules

  • (none specified)

Other Information and Tasks