How to get strings of mathematical operators from the `operator` module in python

Take operator.addfor example:

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

I need a view:

>>>op.add.math_str
"+"

Can I get all these strings math string "+", "-", ">"...module operatorsupported runtime?

EDIT:

>>> [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.',

above code can list most lines of statements, does this mean that I can list lines with re-module?

Thank!

+3
source share
2 answers

No. Create your own vocabulary.

+2
source

You can use the following table from the tutorial to create your own mapping dictionary only once, and then just use it whenever you need it.

+3
source

All Articles