Python 3.7 Python中是否存在“使用”的问题?

Python 3.7 Python中是否存在“使用”的问题?,python-3.7,Python 3.7,每次调用class.function都很累人而且不简洁。有没有一种方法可以直接使用函数调用类中的函数?我正在使用Python 3.7 class SineModel(keras.Model): def __init__(self): super().__init__() ... def np_to_tensor(list_of_numpy_objs): result = (tf.convert_to_tensor(obj) for obj

每次调用class.function都很累人而且不简洁。有没有一种方法可以直接使用函数调用类中的函数?我正在使用Python 3.7

class SineModel(keras.Model):
    def __init__(self):
        super().__init__()
    ...
    def np_to_tensor(list_of_numpy_objs):
        result = (tf.convert_to_tensor(obj) for obj in list_of_numpy_objs)
        return result

您没有正确定义该方法,也没有任何理由将其作为一种方法。嗯,这有点苛刻。只要您只通过类而不是实例调用函数,那么从技术上讲,实例方法和正确的静态方法之间就没有什么区别

也就是说,您几乎不需要为此定义特定的函数

def eval_sine_test(model, optimizer, x, y, x_test, y_test, num_steps=(0, 1, 10)):
    tensor_x_test, tensor_y_test = map(tf.convert_to_tensor, x_test, y_test)
    ...

太容易出虫子了。Javascript有这个功能,并且发现这是一个他们不应该犯的错误。它在严格模式下被禁用。np_to_张量应该是一个静态方法,因为您无论如何都是这样调用它,或者是一个独立函数,因为它似乎没有使用SineModel的任何实例。谢谢,我将使它成为静态的。但是对于SineModel中的函数,有没有一种方法不专门编写SineModel。每次你叫它的时候?不,没有。如果你有一个非常有效的理由将它包含在类中,那就提醒你只定义一个静态方法。我的意思是,如果你计划大量使用它,那么忽略定义你自己的短别名的选项,比如ToTauns= SimMeldel.nptotox张量。
def eval_sine_test(model, optimizer, x, y, x_test, y_test, num_steps=(0, 1, 10)):
    tensor_x_test, tensor_y_test = map(tf.convert_to_tensor, x_test, y_test)
    ...
class SineModel(keras.Model):
    def __init__(self):
        super().__init__()
    ...

    @staticmethod
    def np_to_tensor(*objs):
        result = (tf.convert_to_tensor(obj) for obj in obj)
        return result
def eval_sine_test(model, optimizer, x, y, x_test, y_test, num_steps=(0, 1, 10)):
    tensor_x_test, tensor_y_test = SineModel.np_to_tensor(x_test, y_test)