[Tutor] Help with vars()

Steven Burr sburr@mac.com
Sat, 11 Aug 2001 13:18:08 -0700

On Saturday, August 11, 2001, at 03:08 AM, Charlie Clark wrote:

>> But if you really want to use vars, you can:
>>>>> class Blank:
>> ...     pass
>> ...
>>>>> article = Blank()
>>>>> article.headline = "Horoscope for Peter"
>>>>> article.text = "Things are looking up!"
>>>>> article.asterisk = "Aquarius"
>>>>> print "\n%(headline)s* \n\n%(text)s\n----\n*%(asterisk)s" %
>> vars(article)
>> Horoscope for Peter*
>> Things are looking up!
>> ----
>> *Aquarius
> so vars() acts like a dictionary type wrapper and as such isn't 
> necessary for
> dictionaries themselves?

I'm new to Python myself, so I'm not certain about the terminology and 
would welcome an explanation from one of the gurus.

But first allow me to embarrass myself.  Based on the context in which 
I've seen the term "wrapper" used, I had been thinking that it meant an 
object that includes and extends, or allows easier access to, another 
object.  So for example, if you wrote a class using UserDict and added 
new methods for manipulating the dictionary, that would be a dictionary 
"wrapper."  In 2.2, we'll be able to write class wrappers for 
dictionaries and lists without using UserDict or UserList.

vars(), as I understand it, is a function that returns the __dict__ 
object for any object that contains one.  Since a class instance 
includes a table of its variables and their values, and the table either 
is or can be translated into a dictionary (something else I'm uncertain 
about), calling vars on the instance yields the above result.  I would 
not have thought of that as being a "wrapper," but I could very easily 
be all wet.

it-wouldn't-be-the-first-time'ly yours, sburrious