此前参加了一些生信的开源项目,在项目的代码中知道了“装饰器”的概念,也了解到了python中一些造好的装饰器。参与开源项目,先从python装饰器了解函数式编程开始
装饰器到底实现了什么
实现了函数式编程,讲人话就是做了函数一个外包的壳,数学公式中的f(g(x))中的f()。
def wrapper():
func = function()
func.split()
return wrapper
@wrapper
def hello_world():
value = print("hello,world")
return value
常见的例子
一些官方的装饰器:
import click
@click.command()
@click.option('--count', default=1, help='Number of greetings.')
@click.option('--name', prompt='Your name',
help='The person to greet.')
def hello(count, name):
"""Simple program that greets NAME for a total of COUNT times."""
for x in range(count):
click.echo(f"Hello {name}!")
if __name__ == '__main__':
hello()