开发者

How is the second argument of the eval function different from the third argument?

class all_items(dict):
    def __getitem__(self, key):
       return开发者_运维技巧 1

>>> eval("undefined",dict(),all_items())
1
>>> eval("undefined",all_items(),dict())
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    eval("undefined",all_items(),dict())
  File "<string>", line 1, in <module>
NameError: name 'undefined' is not defined

The all_items class of dictionary should return 1 for any value. Using the eval function, I want "undefined" to evaluate to 1, even though it isn't defined. This works when the all_items dictionary is the third argument of the eval statement, but not when it is the second argument. My question is why doesn't the second statment evaluate to 1? (And how could I make it work?) I'm using Python 2.5.


The second argument must be a dictionary. Implementing the mapping protocol isn't enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜