ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-Tutor
python-Tutor
Re: [Tutor] Help with vars()
by Danny Yoo other posts by this author
Aug 10 2001 9:18AM messages near this date
[Tutor] Help with vars() | Re: [Tutor] Help with vars()
On Fri, 10 Aug 2001, Charlie Clark wrote:

>  I'd like to use vars() in together with a database command (Marc André 

>  Lemburg's mxODBC) but seem to have missed something.

>  

>  articles = {'sternzeichen': 'Wassermann', 'text': 'Es wird besser', 

>  'headline': 'Horoskop f\xc3\xbcr Peter'}

>  

>  when I use

>  insert = "%headline, %text, %sternzeichen" %vars(articles)


Hi Charlie.  Let's take a look:


###
> >> articles = {'sternzeichen': 'Wassermann', 'text': 'Es wird besser',

...             'headline': 'Horoskop f\xc3\xbcr Peter'}
> >> vars(articles)

Traceback (most recent call last):
  File "<stdin> ", line 1, in ?

TypeError: vars() argument must have __dict__ attribute
> >> print vars.__doc__

vars([object]) ->  dictionary

 
Without arguments, equivalent to locals().
With an argument, equivalent to object.__dict__.
###


I see.  vars() is meant to grab all the variables out of something like a
module, but it appears that we can't do the same on a dictionary.  The
error message is somewhat confusing: the online docs should make it more
clear that not just any old object will do... hmmm.  I'll have to think
about a good reason why it behaves that way.


>  I get a TypeError:

>  TypeError: vars() argument must have __dict__ attribute

>  

>  but articles is a dictionary... what screamingly obvious thing am I

>  missing?



Start screaming.  *grin* We won't need to call vars() at all: we can just
do the interpolation directly with the dictionary that's in our hands:

###
> >> insert = "%(headline)s, %(text)s, %(sternzeichen)s" % articles

> >> print insert

Horoskop fr Peter, Es wird besser, Wassermann
###


Note: the string formatting above requires us to put the variables in the
'%(foo)s' sorta format --- the trailing 's' doesn't stand for plurality,
but for 'string'ality.

Hope this helps!


_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Charlie Clark
Danny Yoo
Steven Burr
Charlie Clark
Steven Burr
Charlie Clark
lonetwin
Charlie Clark
Steven Burr

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved