简体   繁体   English

像类构造函数一样python中是否存在函数构造函数

[英]Like a class constructor Is there a function constructor exist in python

Recently I have come across a Python function as follows:最近遇到一个Python函数如下:

def pw_amp(k, x0):
    def _pw_amp(x):
        return cmath.exp(1j * k.dot(x + x0))
    return _pw_amp

Here the _pw_amp seems like a constructor for the function pw_amp .这里_pw_amp似乎是函数pw_amp的构造函数。 But I did not see any function constructor before.但是我之前没有看到任何函数构造函数。 Is it like a class constructor?它像类构造函数吗? Please tell me how _pw_amp works.请告诉我_pw_amp是如何工作的。

This is an instance of a decorator.这是装饰器的一个实例。 Here's a good article explaining them , but the gist is that a decorator modifies an existing function.这是一篇解释它们的好文章,但要点是装饰器修改现有函数。 Eg,例如,

@my_decorator
def my_function():
    ...

Is a fancy way of doing this.是一种奇特的方式。

def my_function():
    ...
my_function = my_decorator(my_function)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM