提问者:小点点

在类中定义函数:Python


我试图在类内部编写两个函数,然后从外部调用它们,如下所示。 这是一种正确的调用函数的方式吗?

class blah:

  def func1(x1, y1):
  return z1

  def func2(x2, y2):
  return z2

model = blah()
df1 = model.func1(1,2)
df2 = model.func1(df1,4)

共3个答案

匿名用户

您应该在类中的函数中添加self

class blah:

    def func1(self, x1, y1):
        z1 = # Your operations
        return z1

    def func2(self, x2, y2):
        z2 = # You operations
        return z2

model = blah()
df1 = model.func1(1,2)
df2 = model.func1(df1,4)

匿名用户

不,您忘了缩进回拨电话:

class blah:

  def func1(self, x1, y1):
    z1 = # you have to asing this before returning
    return z1

  def func2(self, x2, y2):
    z2 = # you have to asing this before returning
    return z2

model = blah()
df1 = model.func1(1,2)
df2 = model.func1(df1,4)

除此之外,它是正确的是。 Python不是硬类型语言,因此您不必定义返回参数的类型。

匿名用户

是啊,你在担心什么?