Cannot pass UserDict to eval()?

Ryan LeCompte rmlecompte at hotmail.com
Mon Aug 14 17:13:46 EDT 2000


From: Ryan LeCompte <rmlecompte at hotmail.com>
To: Frank Niessink <frankn=nuws at cs.vu.nl>
Subject: Re: Cannot pass UserDict to eval()?
Date: Monday, August 14, 2000 4:08 PM


----- Original Message -----
From: Frank Niessink <frankn=nuws at cs.vu.nl>
Newsgroups: comp.lang.python
Sent: Monday, August 14, 2000 3:20 PM
Subject: Cannot pass UserDict to eval()?


> Hi,
>
> This works:
>
> >>> eval('x+1',{},{'x':1})
> 2
>
> But this doesn't:
>
> >>> import UserDict
> >>> u = UserDict.UserDict({'x':1})
> >>> eval('x+1',{},u)
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> TypeError: eval, argument 3: expected dictionary, instance found
>
>
> Why are instances not allowed?
>

Try this:
eval('x+1', {}, u.data)

That will give you what you're looking for. u is just a class instance, and
the 'data' attribute actually contains the dictionary. u is just a wrapper
around the dictionary object.

Ryan LeCompte
rmlecompte at hotmail.com






-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list