how to get all the "variables" of a string formating?

Calvin Spealman ironfroggy at gmail.com
Wed Dec 6 13:53:11 EST 2006


On 6 Dec 2006 18:33:26 GMT, Duncan Booth <duncan.booth at invalid.invalid> wrote:
> "Calvin Spealman" <ironfroggy at gmail.com> wrote:
>
> > I am not aware of anything in the stdlib to do this easily, but its
> > pretty easy to get them. See this example:
> >
> > class format_collector(object):
> >    def __init__(self):
> >       self.names = []
> >    def __getitem__(self, name):
> >       self.names.append(name)
> >       return ''
> >
> > collector = format_collector()
> > "%(foo)s %(bar)s" % collector
> > assert collector.names == ['foo', 'bar']
> >
> > Of course, wrapping this functionality into a simple function is
> > straightforward and will look better.
>
> You should return a value like 0 instead of ''.
>
> >>> collector = format_collector()
> >>> "%(foo)s %(bar)d" % collector
>
> Traceback (most recent call last):
>   File "<pyshell#3>", line 1, in <module>
>     "%(foo)s %(bar)d" % collector
> TypeError: int argument required
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Good thinking with returning 0 and using a set. Maybe something added
to the stdlib would be welcome instead of anyone needing it writing
this little class?

-- 
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/



More information about the Python-list mailing list