repr = expression representation?

Christian Gollwitzer auriocus at gmx.de
Fri May 17 02:57:50 EDT 2019


Am 17.05.19 um 06:13 schrieb Stefan Ram:   However, look at this
> 
> |>>> print( str( print ))
> |<built-in function print>
> 
> |>>> print( repr( print ))
> |<built-in function print>
> 
>    . While it is nice that »str( print )« gives some useful
>    information, I would expect »repr( print )« to give
>    »print« - 

This is impossible. Python does not use "call by name", so a function 
cannot know how the argument is called in the upper stack level. Consider:


Apfelkiste:inotes chris$ python3
Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> blafasel = print
 >>> print(repr(blafasel))
<built-in function print>
 >>>

You'll have to accept that not all Python objects can be represented as 
literals. While a user defined function /could/ be printed as a lambda, 
so expecting:

def test(x):
	return 2*x

print(repr(test))

-> lambda x : 2*x

would be half-reasonable, it is impossible to print out the C source 
code of the built-in print function, unless one builds a JIT C compiler 
into Python.

	Christian


More information about the Python-list mailing list