Redefining python built-in function

后端 未结 3 1596
花落未央
花落未央 2021-01-02 20:08

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)
         


        
相关标签:
3条回答
  • 2021-01-02 20:26

    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:

    >>> def foo():
    ...     str = 0
    ...     return str
    ... 
    >>> 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
    >>> def foo():
    ...     return str(0)
    ... 
    >>> foo()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in foo
    TypeError: 'int' object is not callable
    >>> 
    

    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
    >>> del str
    >>> str
    <class 'str'>
    >>> 
    
    0 讨论(0)
  • 2021-01-02 20:35

    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:

    def blah():
        x = str(12)
        str = "asdf asdf asdf"
        doStuff(str)
    

    will fail with an UnboundLocalError.

    0 讨论(0)
  • 2021-01-02 20:42

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

    >>> str = 'Hello world!'
    >>> print str
    Hello world!
    str(str)
    Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
    TypeError: 'str' object is not callable
    

    EDIT:

    Here is a simple demo:

    def salut():
       str = 'Hello world!'
       return str
    if __name__ == '__main__':
       s = salut()
       print str(s) #nothing prevents you from using 'str' outside 'salut()'
    
    0 讨论(0)
提交回复
热议问题