Python: getting a reference to a function from inside itself

后端 未结 3 2086
小鲜肉
小鲜肉 2021-02-12 13:03

If I define a function:

def f(x):
    return x+3

I can later store objects as attributes of the function, like so:

f.thing=\"he         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-02-12 13:42

    The same way, just use its name.

    >>> def g(x):
    ...   g.r = 4
    ...
    >>> g
    
    >>> g(3)
    >>> g.r
    4
    

提交回复
热议问题