Skip to content Skip to sidebar Skip to footer

Redefining Python Built-in Function

I'm working on a python program and the author has written a function that looks like this def blah(): str = 'asdf asdf asdf' doStuff(str) This seems to work, even though

Solution 1:

Internally, the function's local variable table will contain an entry for str, which will be local to that function. You can still access the builtin class within the function by doing builtins.str in Py3 and __builtin__.str in Py2. Any code outside the function will not see any of the function's local variables, so the builtin class will be safe to use elsewhere.

There is another caveat/corner case here, which is described in this question. The local table entry is created at compile-time, not at runtime, so you could not use the global definition of str in the function even before you assign "asdf asdf asdf" to it:

defblah():
    x = str(12)
    str = "asdf asdf asdf"
    doStuff(str)

will fail with an UnboundLocalError.

Solution 2:

This seems to work, even though str is a built in function and shouldn't be used as a variable.

Yes, that is true. Python doesn't stop you from shooting yourself in the foot. It's up to you as the developer to make sure your not overwriting builtin names.

What is actually happening here? My guess is str will no longer be usable as a function, but only in the scope of the blah() function he's written. Is that correct? This won't redefine str globally, right?

Your are partially correct here as well. If the value of str is overwritten local, then only the current scope is affected. The global value of str remains unchanged. However, if str is over written in the global scope, then it affects all sub-scopes. The reason behind this is how the Python interpreter compiles values at run-time. This behavior can be observed using a simple example:

>>>deffoo():...str = 0...returnstr...>>>foo()
0
>>>str(0)
'0'
>>>

The first example works because str is only overwritten in the scope of foo(). This second example fails however because str is overwritten globally:

>>> str = 0>>> deffoo():
... returnstr(0)
... >>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in foo
TypeError: 'int'objectisnotcallable>>> 

You can always import builtins(__builtins__ in Python 2) though, and reset the value of str to its original meaning:

>>>str = 0>>>str
0
>>>import __builtins__>>>str = __builtins__.str>>>str
<type 'str'>
>>>str(0)
'0'
>>>

Also, as @Brad Solomon stated, you can simply use del str to recover the builtin str value:

>>>str = 0>>>str
0
>>>delstr>>>str
<class 'str'>
>>>

Solution 3:

In your case, str is just a variable and nothing prevents you from the usual use of str() outside that function:

>>> str = 'Hello world!'>>> printstr
Hello world!
str(str)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: 'str'objectisnotcallable

EDIT:

Here is a simple demo:

defsalut():
   str = 'Hello world!'returnstrif __name__ == '__main__':
   s = salut()
   printstr(s) #nothing prevents you from using 'str' outside 'salut()'

Post a Comment for "Redefining Python Built-in Function"