Python/PyCharm 缺少参数 'self'?

标签 python pycharm

我目前正在学习 Python,我不明白我的代码有什么问题,但是 PyCharm 一直给我以下错误:

Traceback (most recent call last):
File "C:/Users/Sam/PycharmProjects/untitled1/app.py", line 5, in <module>
    fish1.bubbles()
TypeError: bubbles() missing 1 required positional argument: 'self'

这是我的代码:

import random
from Fish import Fish

fish1 = Fish
fish1.bubbles()

fish1.name = input("enter the name of your fish: ")
fish1.coords = ("({0},{1})".format(random.randint(1, 100), random.randint(1, 100)))

print("The fish's name is {0}, and it is swimming at co-ordinates{1}".format(fish1.name, fish1.coords))

这是我的 Fish.py 文件:

class Fish:

    def __init__(self, name, coords):
        self.name = name
        self.coords = coords

    def bubbles(self):
        print("{0} blew some bubbles".format(self))

如有任何帮助,我们将不胜感激!

最佳答案

您在 Creating Instance Objects 中有错误

在本节 fish1 = Fish 中,您没有创建实例,但尝试使用它 fish1.bubbles()

因此尝试将 fish1 = Fish 更改为 fish1 = Fish(name, coords)

namecoords 需要在构造函数中。

关于Python/PyCharm 缺少参数 'self'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53445651/

相关文章:

创建 Git 分支时 PyCharm 自动拉取

python - 向量化计算矩阵和向量之间的欧氏距离

python - 从字符串中解析和组合日期和时间(闰秒) - python

python - 如何使用 python 将表单发布到 aspx 网站

python - 配置 PyCharm IDE 以按照 Eclipse 的方式组织导入(Django 开发)

Pycharm错误: Cannot determine module type ("WEB_MODULE")

windows - 为什么 git bash 在 PyCharm 上表现怪异?

python - 关于UnboundLocalError : local variable 'font_size' referenced before assignment in Python

python - scikit-learn 中的随机分层 k 折交叉验证?

python - Python 处理二进制文件有危险吗?