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)
解决方案
您应该在类内的函数中添加 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)
推荐阅读
- python - 如何将 Python 解释器添加到 .Net Core Docker 映像?
- css - 调整屏幕上的 div 从 1200 像素(及以上)更改为移动
- laravel - 在 Ubuntu 中安装 Laravel 后输入命令 php artisan ser 无法打开流:
- javascript - 获取将图片上传为base64而不是文件
- verilog - 错误:(vsim-3033) Ripple_counter.v(22):“JK_flip_flop”的实例化失败。未找到设计单位
- javascript - 我如何在 JS 中转换这个挑战?
- javascript - 无法为 document.getElementsByClassName() 定位的元素应用样式
- spring - 迁移到 SpringBoot 2 Mockito 3.x 后集成测试套件失败
- python - 为什么模型表单集不保存在 django 中
- tabs - 在 Microsoft 团队的自定义应用程序中添加选项卡