How can a function access its own attributes?

前端 未结 16 1792
再見小時候
再見小時候 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:57

    As a workaround you could use a factory function to fix your scope:

    def factory():
        def inner():
            print inner.x
        return inner
    
    
    >>> foo=factory()
    >>> foo.x=11
    >>> foo()
    11
    >>> bar = foo
    >>> del foo
    >>> bar()
    11
    

提交回复
热议问题