Ale*_*oks 5 python code-analysis functional-programming
是否有一个工具可以让您将函数/方法注释为"纯",然后分析代码以测试所述函数/方法是否是无副作用的?
Ray*_*ger 10
在Python世界中,问题没有多大意义,因为对象在函数调用中发生了很多事情.
例如,如何判断以下函数是否纯粹?
def f(x):
return x + 1
Run Code Online (Sandbox Code Playgroud)
答案取决于x是什么:
>>> class A(int):
def __add__(self, other):
global s
s += 1
return int.__add__(self, other)
>>> def f(x):
return x + 1
>>> s = 0
>>> f(A(1))
2
>>> s
1
Run Code Online (Sandbox Code Playgroud)
虽然函数f看起来很纯,但x上的add运算有增加s的副作用.
| 归档时间: |
|
| 查看次数: |
590 次 |
| 最近记录: |