[Tutor] Help with vars()

Steven Burr sburr@mac.com
Sat, 11 Aug 2001 01:20:37 -0700


On Friday, August 10, 2001, at 02:18 AM, Danny Yoo wrote:

> On Fri, 10 Aug 2001, Charlie Clark wrote:
>
>> I'd like to use vars() in together with a database command (Marc =
Andr=E9
>> Lemburg's mxODBC) but seem to have missed something.
>>
>> articles =3D {'sternzeichen': 'Wassermann', 'text': 'Es wird besser',
>> 'headline': 'Horoskop f\xc3\xbcr Peter'}
>>
>> when I use
>> insert =3D "%headline, %text, %sternzeichen" %vars(articles)
>
[snip]
> Start screaming.  *grin* We won't need to call vars() at all: we can=20=

> just
> do the interpolation directly with the dictionary that's in our hands:
>
> ###
>>>> insert =3D "%(headline)s, %(text)s, %(sternzeichen)s" % articles
>>>> print insert
> Horoskop fr Peter, Es wird besser, Wassermann
> ###

But if you really want to use vars, you can:

 >>> class Blank:
...     pass
...
 >>> article =3D Blank()
 >>> article.headline =3D "Horoscope for Peter"
 >>> article.text =3D "Things are looking up!"
 >>> article.asterisk =3D "Aquarius"
 >>> print "\n%(headline)s* \n\n%(text)s\n----\n*%(asterisk)s" %=20
vars(article)

Horoscope for Peter*

Things are looking up!
----
*Aquarius