Re: 动态生成函数
cafeeee <[email protected]>
| Newsgroups | gmane.comp.python.chinese |
|---|---|
| Message-ID | <[email protected]> |
用闭包可以解决这个问题:
>>> def s(param): print param
...
>>> m={"a":1, "b":2, "c": None}
>>> for k,v in m.items():
... def ss(param=v): s(param)
... locals()[k] = ss
...
>>> a()
1
>>> b()
2
>>> c()
None
>>>
_______________________________________________
python-chinese
Post: send [email protected]
Subscribe: send subscribe to [email protected]
Unsubscribe: send unsubscribe to [email protected]
Detail Info: http://python.cn/mailman/listinfo/python-chinese