首页 > 解决方案 > 在类中定义函数

问题描述

我正在尝试在一个类中编写两个函数并从外部调用它们,如下所示。这是调用函数的正确方法吗?

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)

标签: python

解决方案


您应该在类内的函数中添加 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)

推荐阅读