1

Now I just tried to test if I can call built-in function inside overrided function in Python3, as follows:

def print(val=0):
    print("TEST")  # How can I call the original `print()` here to avoid RecursionError


print(10)

But the following error thrown. It makes sense.

>>> RecursionError: maximum recursion depth exceeded

I am curious if there is ways to call an original built-in function inside its overrided function, such as sys.__builtin__.print() I guess.

Thank you.

Park
  • 2,446
  • 1
  • 16
  • 25
  • 2
    While there are answers that answer your question, let us also mention: you should not do this! :) – Tim Feb 15 '22 at 06:26
  • Does this answer your question? [How to get back an overridden python built-in function?](https://stackoverflow.com/questions/20885760/how-to-get-back-an-overridden-python-built-in-function) – pippo1980 Feb 15 '22 at 06:27
  • @pippo1980 Thank you very much for the information. It was helpful but the answer is for Python2. However, I wanstd to know it for python3. I updated my question title to Python3. – Park Feb 15 '22 at 06:30
  • @Tim-Erwin Sorry, I don't get it. Did I do something wrong? – Park Feb 15 '22 at 06:31
  • It is bad practice to shadow the built in function names. Most IDE will give you a warning for this and most colleagues will hate you for it ;) – endive1783 Feb 15 '22 at 07:12
  • What I mean is, you should not override builtin functions in general. – Tim Feb 15 '22 at 07:12
  • @endive1783 Yes, that's right. I just wanted know how it works. My IDE PyCharm also show warning :) – Park Feb 15 '22 at 07:50
  • 2
    @Tim-Erwin Thanks, I just wanted to know if it is possible or not in Python3. Not gonna use it. Thank you :) – Park Feb 15 '22 at 07:51

4 Answers4

3

Builtins are available in a module called builtins. Makes sense!

import builtins

def print(val=0):
    builtins.print("TEST")  # How can I call the original `print()` here to avoid RecursionError

print(10)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
1

You can use the builtins module to reference the built in print

import builtins
def print(val=0):
    builtins.print('TEST')
endive1783
  • 827
  • 1
  • 8
  • 18
1

I am posting same answer because I was beaten only by 2 minutes:

this solution in for python3 only python2 used import __builtin__

import builtins 

def print(val=0):
    builtins.print("TEST")  # How can I call the original `print()` here to avoid RecursionError


print(10)

output:

TEST

this works too:

def print(val=0):
    global print
    del print 
    print("TEST")  # How can I call the original `print()` here to avoid RecursionError


print(10)

see here for duplicated post

How to get back an overridden python built-in function?

pippo1980
  • 2,181
  • 3
  • 14
  • 30
  • 1
    Thank you for the additional information using `global` and `del`. Interesting!, Upvoted. – Park Feb 15 '22 at 06:34
0

try this

import __builtin__

def print(val=0):    
     __builtin__.print('TEST')


print(10)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 15 '22 at 07:21
  • 1
    Make sure to test your code before posting. There is no `importable __builtin__` module, but there is a `__builtins__` module auto-imported for you. – tdelaney Feb 15 '22 at 16:33