How to deal with the problem that a function parameter name is the same as another function in Python?

Here is an example:

def clean_datetime(x):
    return x

def func(clean_datetime = True):
    if clean_datetime:
        return clean_datetime(1)
        
func(True)

This will return an error TypeError: 'bool' object is not callable. Is there a way I don't need to change the function parameter name?


Solution 1:

Technically, you could pull the function out of globals(), but this is a terrible, terrible thing to do compared to just renaming the function or the parameter.

In [53]: def clean_datetime(x):
    ...:     return x
    ...:
    ...: def func(clean_datetime = True):
    ...:     if clean_datetime:
    ...:         return globals()['clean_datetime'](1)
    ...:
    ...: func(True)
Out[53]: 1