Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override default python functions like round()?

I Want to override default round() function of python because I have to convert the result of round() function into integer. By default round() returns value in float. The code given below is returning error message.

RuntimeError: maximum recursion depth exceeded while calling a Python object.

def round(number):
     if type(number) is float: return int(round(number))
     return None
like image 669
yash lodha Avatar asked Sep 28 '15 11:09

yash lodha


People also ask

Can you override built in functions in Python?

Python has a number of built-in functions that are always accessible in the interpreter. Unless you have a special reason, you should neither overwrite these functions nor assign a value to a variable that has the same name as a built-in function.

What is the round function in Python?

Python round() Function The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals. The default number of decimals is 0, meaning that the function will return the nearest integer.


2 Answers

The issue with your current code is that after you have overwritten the built-in round() method, when you call round() inside your own round() , you are recursively calling your own function, not the built-in round function.

For Python 3.x, you can use builtins module to access the round() built-in function -

import builtins
def round(number):
    if type(number) is float: return int(builtins.round(number))
    return None

For Python 2.x , it would be __builtin__ module -

import __builtin__
def round(number):
    if type(number) is float: return int(__builtin__.round(number))
    return None

Though I would really suggest not doing this, rather use a new name for your round function, maybe something like round_int or so.

Please note, another thing is that your code would return the rounded number for float type, for all other types, it would return None , I am not sure if this is intentional or not, but I am guessing you would want to return back the number for other types (atleast int?) .

like image 167
Anand S Kumar Avatar answered Oct 12 '22 23:10

Anand S Kumar


As already pointed out, round refers to your function not to the builtin function as you have shadowed the function and as python looks in the local, enclosing, global scope and finally builtins in that order it see round refers to your function so your function essentially keeps calling itself until you hit a recursion limit.

I would avoid shadowing the round function at all but a much simpler approach would be a try/except adding .5 to the number passed in and catching a TypeError:

def round(number):
    try:
        return int(number + .5)
    except TypeError:
        return None

If you were going to check types, you should use issinstance, you can combine it with numbers.Number to check for any numeric type:

from numbers import Number
def round(number):
    if isinstance(number, Number):
        return int(number+.5)
    return None

If you just wanted to check for a float type:

def round(number):
    if isinstance(number, float):
        return int(number+.5)
    return None
like image 37
Padraic Cunningham Avatar answered Oct 12 '22 22:10

Padraic Cunningham