How can a function access its own attributes?

前端 未结 16 1809
再見小時候
再見小時候 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 07:50

    The answer is rather simple. Just use the fact name is looked for at execution time, not compile time:

    def f():
        return f._x
    
    f._x = "foo"
    f()           # -> "foo"
    

提交回复
热议问题