简体   繁体   English

将参数传递给 class 方法

[英]Passing an argument to a class method

I am trying to get to grips with working with objects in python and have encountered an error which is confusing me.我正在尝试处理 python 中的对象,但遇到了一个令我困惑的错误。 I have added the following property to a class:我已将以下属性添加到 class:

@property
def train(self, index):
  return self.make_dataset(self.train_df[index])

I want to pass it the argument 'index' so that I can call this function during a training loop.我想将参数'index'传递给它,以便我可以在训练循环期间调用这个 function。 However when I try this with the following code, I get the error:但是,当我使用以下代码尝试此操作时,出现错误:

train() missing 1 required positional argument: 'index'

Even though I did pass the argument as below.即使我确实通过了以下论点。 I have been looking on the internet but still can't work out what the route cause of the warning is.我一直在网上寻找,但仍然无法弄清楚警告的原因是什么。 Is this an incorrect way to mass an argument to a method?这是将参数集中到方法的错误方法吗?

for train_loop in range(len(train_df)):
    history = model.fit(multi_window.train(train_loop))

Remove the @property decorator:删除 @property 装饰器:

def train(self, index):
    return self.make_dataset(self.train_df[index])

This will make it an instance method, callable by an object of the class (if that's what you'd like it to be).这将使它成为一个实例方法,可由 class 的 object 调用(如果这是您想要的)。

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

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