simple question about classes
Bjorn Pettersen
BPettersen at NAREX.com
Mon Aug 13 16:45:58 EDT 2001
> From: Scott Hathaway [mailto:scott_hathaway at riskvision.com]
>
> If I have a class, is there a way to iterate through the name
> of all of the
> properties (public variables) of the class which would
> include public vars
> that were added at the time of instantiation?
>
> class a():
> getPublicVars(self):
> # loop here that prints each variable and it's value
>
>
> myClassVar = a()
> myClassVar.color = 'red'
> myClassVar.style='antique'
> myClassVar.getPublicVars()
>>> class C:
... def __init__(self):
... self.x = 1
... self.y = 2
...
... def getVars(self):
... for k,v in self.__dict__.items():
... print 'self.'+k, '=', v
...
>>> c = C()
>>> c.z = 'hello world'
>>> c.getVars()
self.z = hello world
self.x = 1
self.y = 2
>>>
whether-that-makes-them-public-is-up-to-you'ly y'rs
-- bjorn
More information about the Python-list
mailing list