Re: [Tutor] Help with vars()
by Steven Burr other posts by this author
Aug 11 2001 8:20AM messages near this date
Re: [Tutor] Help with vars()
|
Re: [Tutor] Help with vars()
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é
> > 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)
>
[snip]
> 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
> ###
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
_______________________________________________
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
|