use var to form name of object

gel geli at tasmail.com
Wed Jul 5 22:50:06 EDT 2006


Simon Forman wrote:

> 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

Yeah I am still getting my head around things... not exactly sure what
you where saying about the globals, but this works


global k
k = 5
class foo:

        def wow(self, n):
                global k
                k += n
                return k


f=foo()
f.wow(55)




More information about the Python-list mailing list