How can a function access its own attributes?

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

    I doubt this is the best way to accomplish this, but you can access the attributes by using the method's name within the method:

    >>> def foo():
    ...   print foo.x
    ... 
    >>> foo()
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 2, in foo
    AttributeError: 'function' object has no attribute 'x'
    >>> foo.x = 5
    >>> foo()
    5
    

提交回复
热议问题