Using print as a variable name in python

Following the tutorial for python, I found out that we can use print for the variable name, and it works fine. But after assigning a print variable, how can we return the original print function?

>>> print("Hello World!!") Hello World!!! >>> print = 5 >>> print("Hi") 

Now the last call throws a TypeError error : the 'int' object cannot be called , since now the print has an integer value of 5.

But how can we return the original print functionality now? Should we use the class name for the print function or something else? Like in SomeClass.print("Hi") ?

Thanks in advance.

+4
source share
3 answers
 >>> print = 5 >>> print = __builtins__.print >>> print("hello") hello 
+18
source

In fact, you can remove the variable so that the inline function works again:

 >>> print = 5 >>> print('cabbage') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not callable >>> del print >>> print('cabbage') cabbage 
+7
source

If you want to use it as a temporary method, execute them, but after that apply print to the print variable:

 print = __builtins__.print 
+5
source

Source: https://habr.com/ru/post/1492556/


All Articles