python错误 __str__() Takes 0 positional arguments but 1 was given
扔个三星炸死你
扔个三星炸死你 2017-06-15 09:22:17
0
1
1974
# -*- coding:gb2312 -*-
class car:
    # 属性
    
    # 方法
    def __str__():
        print("哈哈哈哈哈哈啊哈")
    
    def move():
        print("车在移动。")
    def Whistle():
        print("车载鸣笛。")

BMW = car()

print(BMW)

Error message:

Google translated it and said that it takes 0 position parameters, but it gave 1
I don’t understand what this means.

扔个三星炸死你
扔个三星炸死你

reply all(1)
洪涛

The functions defined in class need to provide a positional parameter of self, because when the class is instantiated, the instance object will be passed in and then bound to the function, so the code should be adjusted to:

# -*- coding:gb2312 -*-
class car:
    # 属性

    # 方法
    def __str__(self):
        return ("哈哈哈哈哈哈啊哈")

    def move(self):
        print("车在移动。")

    def Whistle(self):
        print("车载鸣笛。")


BMW = car()

print(BMW)

For the relationship between method and function, you can refer to my article: Python: The difference between functions and methods

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template