use var to form name of object

Simon Forman rogue_pedro at yahoo.com
Wed Jul 5 22:17:20 EDT 2006


gel wrote:

<snip>

> class testclass:
>     import wmi
>     import time
>     global d_software
>     global l_notepad
>     global d_licence_numbers
>     d_licence_numbers = {"notepad.exe":1, "Adobe":1}
>     l_notepad =[]
>     d_software = {"notepad.exe":[[],[]], "Adobe":[[],[]]}
>


Wow!  For a second there I thought you could make a "global" class
attribute in a way I'd never seen before...

But it's turns out you can't.

>>> k = 0
>>> class foo:
	global k
	def wow(self, n):
		self.k += n


>>> foo.k

Traceback (most recent call last):
  File "<pyshell#31>", line 1, in -toplevel-
    foo.k
AttributeError: class foo has no attribute 'k'
>>> f = foo()
>>> f.k

Traceback (most recent call last):
  File "<pyshell#32>", line 1, in -toplevel-
    f.k
AttributeError: foo instance has no attribute 'k'
>>> f.wow(23)

Traceback (most recent call last):
  File "<pyshell#33>", line 1, in -toplevel-
    f.wow(23)
  File "<pyshell#29>", line 4, in wow
    self.k += n
AttributeError: foo instance has no attribute 'k'
>>>


BTW,  that's a weird place to put import statements.

Peace,
~Simon




More information about the Python-list mailing list