What is Python's coerce() used for?

后端 未结 2 1647
南方客
南方客 2021-01-01 11:59

What are common uses for Python\'s built-in coerce function? I can see applying it if I do not know the type of a numeric value as per the document

2条回答
  •  长发绾君心
    2021-01-01 12:25

    Python core programing says:

    Function coerce () provides the programmer do not rely on the Python interpreter, but custom two numerical type conversion."

    e.g.

    >>> coerce(1, 2)
    (1, 2)
    >>>
    >>> coerce(1.3, 134L)
    (1.3, 134.0)
    >>>
    >>> coerce(1, 134L)
    (1L, 134L)
    >>>
    >>> coerce(1j, 134L)
    (1j, (134+0j))
    >>>
    >>> coerce(1.23-41j, 134L)
    ((1.23-41j), (134+0j))
    

提交回复
热议问题