How can a function access its own attributes?

前端 未结 16 1805
再見小時候
再見小時候 2020-11-28 07:49

is it possible to access the python function object attributes from within the function scope?

e.g. let\'s have

def f():
    return          


        
16条回答
  •  情歌与酒
    2020-11-28 08:14

    Sorry for the late reply but I just stumbled upon this. I would have to argue that the way that “g” is asked to work is non-Pythonic. Inside function, the name “f“ refers to the value of a global variable at the time the function is called. Given that, consider the following:

    def f():
        print(f)
    f, g = 42, f
    g()  # prints 42
    del f
    g()  # raises an exception 
    

    Hopefully, no one argues that this is incorrect behavior. Given that fact, I cam only vote for any answer that requires the use of a different variable name (e.g. “self”) inside the function.

提交回复
热议问题