2011-06-13 40 views
3

operator.add例如:如何讓蟒蛇的數學運算符串從模塊`operator`

>>>import operator as op 
>>>op.add(1,2)  #means 1 + 2 
3 
>>>op.add.__name__ 
'add' 

我要排序的:

>>>op.add.math_str 
"+" 

我可以得到所有這些數學串"+", "-", ">"...模塊operator支持運行時?

編輯:

>>> [eval(x) for x in [".".join(("op",x,"__doc__")) for x in dir(op)]] 
['abs(a) -- Same as abs(a).', 
'add(a, b) -- Same as a + b.', 
'and_(a, b) -- Same as a & b.', 
'concat(a, b) -- Same as a + b, for a and b sequences.', 
'contains(a, b) -- Same as b in a (note reversed operands).', 
'delitem(a, b) -- Same as del a[b].', 
'delslice(a, b, c) -- Same as del a[b:c].', 
'div(a, b) -- Same as a/b when __future__.division is not in effect.', 
'str(object) -> string\n\nReturn a nice string representation of the object.\nIf the argument is a string, the return value is the same object.', 

上面的代碼可以列出大多數運營商的字符串時,這意味着我可以列出與重新組件串?

謝謝!

回答

2

不可以建立你自己的字典。

+0

以下是運營商文檔:http://docs.python.org/library/operator.html#mapping-operators-to-functions – Tim 2011-06-13 08:54:31