Python比较函数

Jas*_*uit 4 python comparison functional-programming

我有一些数据,适合于表示作为一个值和比较功能,(val, f),所以另一个值可以反对通过查看检查f(val, another)True.这很简单.

他们中有些人只需要>,<==作为f,但是,我无法找到使用它们的清洁方式; 我最终写的东西就像ScorePoint(60, lambda a, b: a <= b).那很难看.

有没有办法可以做更像的事情ScorePoint(60, <=)

Ric*_*dle 11

operator模块是你的朋友:

import operator
ScorePoint(60, operator.le)
Run Code Online (Sandbox Code Playgroud)

请参见http://docs.python.org/library/operator.html