简体   繁体   中英

What is the echo of function in ipython interactive mode?

I know the interactive model output is usually __repr__ method return.

But in IPython or jupyter it dosen't.

for example:

In [1]: abs
Out[1]: <function abs(x, /)>

In [2]: repr(abs)
Out[2]: '<built-in function abs>'

In [3]: str(abs)
Out[3]: '<built-in function abs>'

In original python:

>>> abs
<built-in function abs>

How can I get the <function abs(x, /)> string in program?

Below is the result of testing repr() on a number of platforms. All returns the same description: <built-in function abs>

Test code:

import sys, platform
print("repr:  ", getattr(__builtins__, "abs").__repr__()); \
print("os:    ", platform.system(), platform.release()); \
print("python:", sys.version, sys.version_info); 

Result:

iPython online (rollapp.com)
----------------------------
repr:   <built-in function abs>
os:     Linux 4.15.0-137-generic
python: 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)


Interactive IPython 7.8.0 browser (pythonanywhere.com)
------------------------------------------------------
repr:   <built-in function abs>
os:     Linux 5.4.0-1029-aws
python: 3.8.0 (default, Nov 14 2019, 22:29:45)
        [GCC 5.4.0 20160609] sys.version_info(major=3, minor=8, micro=0, releaselevel='final', serial=0)

Jupyter online (jupyter.org)
----------------------------
repr:   <built-in function abs>
os:     Linux 4.19.150+
python: 3.n7.8 | packaged by conda-forge | (default, Nov 17 2020, 23:42:15)
        [GCC 7.5.0] sys.version_info(major=3, minor=7, micro=8, releaselevel='final', serial=0)


Jupyter online (cocalc.com)
---------------------------
repr:   <built-in function abs>
os:     Linux 5.4.0-1036-gcp
python: 3.8.5 (default, Jan 27 2021, 15:41:15) 
        [GCC 9.3.0] sys.version_info(major=3, minor=8, micro=5, releaselevel='final', serial=0)


macOS Catalina, python 2.7 (local)
----------------------------------
repr:   <built-in function abs>
os:     Darwin 19.6.0
python: 2.7.16 (default, Jun  5 2020, 22:59:21) 
        [GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- sys.version_info(major=2, minor=7, micro=16, releaselevel='final', serial=0)


macOS Catalina, python 3.8 (local)
----------------------------------
repr:   <built-in function abs>
os:     Darwin 19.6.0
python: 3.8.8 (default, Feb 26 2021, 23:59:43) 
        [Clang 12.0.0 (clang-1200.0.32.29)] sys.version_info(major=3, minor=8, micro=8, releaselevel='final', serial=0)


Python 3.4 online (onlinegdb.com)
---------------------------------
repr:   <built-in function abs>
os:     Linux 4.15.0-1044-gcp
python: 3.4.3 (default, Nov 12 2018, 22:25:49)
        [GCC 4.8.4] sys.version_info(major=3, minor=4, micro=3, releaselevel='final', serial=0)


Python 3.8 online (repl.it)
---------------------------
repr:   <built-in function abs>
os:     Linux 5.4.0-1042-gcp
python: 3.8.9 (default, Apr  3 2021, 01:00:00) 
        [GCC 7.5.0] sys.version_info(major=3, minor=8, micro=9, releaselevel='final', serial=0)

You can get the built in attribute name with getattr(__builtins__, "abs") and the string representation using the ordinary str() ie:

>>> f = getattr(__builtins__, "abs")

>>> f
<built-in function abs>

>>> type(f)
<class 'builtin_function_or_method'>

>>> fstr = str(f)

>>> fstr
'<built-in function abs>'

>>> type(fstr)
<class 'str'>

or python < 3

getattr(__builtin__, "abs")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM