Apologies upfront if this is a dupe; I search for "_curried python" and got 14 results, and then simply _curried" and that only bumped up to 33 results, and none seemed to help out...
问题:今天我在我们的代码库中遇到了一个我最初认为是错误的地方,这是嫌疑犯:
student.recalculate_gpa()
现在,我怀疑这是一个打字错误,因为student
是一个没有recalculate_gpa
方法的Student
类的实例。但是,它有一个calculate_gpa
方法:
(其中User
是标准的django用户类)但是,代码没有出错,这对我来说毫无意义。所以我检查了一下,发现了这个:
... (a bunch of methods)
('calculate_gpa', <unbound method Student.calculate_gpa>),
... (some more methods)
('recalculate_gpa', <unbound method Student._curried>),
奇怪的是,recalculate_gpa
实际上是一种方法。但它究竟从何而来?我在我们的代码库中搜索“_curried”,但什么也没找到,所以这一定是一些Django相关的行为。当然,我希望在我们的项目中的某个地方,我们已经描述了动态命名函数是如何形成的,因为recalculate
似乎是calculate
的一个看似合理的派生词,但是我真的不知道从哪里开始寻找。在
因此,我的问题是:如何生成像上面这样的curry方法,我应该从哪里开始寻找我们自己的代码库是如何curry的?在
先谢谢你!在
curried方法是在实际调用方法之前部分调用它
例如
您还可以使用lambda轻松实现它
^{pr2}$虽然我不确定这是否与您的实际问题有关。。。在
django还提供了一个非常类似于functools.partial在
^{3}$(来自https://stackoverflow.com/a/25915489/541038)
所以您可能需要查找
Student.recalculate_gpa =
或者在
Student.__init__
方法中为self.recalculate_gpa =
你可能不会发现它在寻找
def recalculate_gpa
相关问题 更多 >
编程相关推荐