简体   繁体   English

在 python 中是否有理由在 class 中将 self 传递给 static 方法?

[英]In python is there a reason to pass self to static method in a class?

I was reviewing code at One-Class Neural Network .我在One-Class Neural Network审查代码。

The author creates a @staticmethod :作者创建了一个@staticmethod

@staticmethod
def custom_ocnn_loss(self,nu, w, V):

but later inside the OC_NN fit instance method, accesses it using the class name and passes in self :但稍后在OC_NN fit实例方法中,使用 class 名称访问它并传入self

model.compile(loss=OC_NN.custom_ocnn_loss(self,nu, w, V), optimizer=opt,metrics=None)

Is there a reason to do this (ie create @staticmethod which takes self as an argument and is accessed inside instance method using class name)?是否有这样做的理由(即创建以self作为参数并使用@staticmethod名称在实例方法内部访问的@staticmethod)?

In the script, custom_ocnn_loss function has @staticmethod decorator which means it is not an instance method but a static method.在脚本中, custom_ocnn_loss function 具有@staticmethod装饰器,这意味着它不是实例方法,而是 static 方法。 The variable self might suggest differently, but when calling the custom_ocnn_loss function, the self variable is passed to it.变量self可能建议不同,但是当调用custom_ocnn_loss function 时, self变量会传递给它。

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

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