Accessing class instance variables with vars().

Andrew Bennetts andrew-pythonlist at puzzling.org
Tue Feb 4 21:51:02 EST 2003


On Wed, Feb 05, 2003 at 02:34:08AM +0000, Arcady Genkin wrote:
> How would I make the following code print both class variable 'foo'
> and the local variable 'bar' through a variable mapping?
> 
> class A:
>     foo = 1
>     def show( self ):
>         bar = 2
>         print '%(foo)d %(bar)d' % vars(self)
> 
> This does not work since vars(self) returns an empty dictionary.

As you say, foo is a class variable, not an instance variable.  Try
vars(self.__class__) or vars(A).

-Andrew.






More information about the Python-list mailing list