我是python编程的初学者。我一直遇到一个类型错误:object()不接受任何参数,我不知道如何修复它。我该怎么办?

2024-10-04 01:26:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python编程的初学者。我一直遇到一个类型错误:object()不接受任何参数,我不知道如何修复它。我该怎么办

这是我的密码

import matplotlib.pyplot as plt
%matplotlib inline


class Rectangle(object):
    def _init_(self, height, width, color):
        self.height = height
        self.width = width
        self.color = color

    def add_height(self,h):
        self.height = self.height + h
        return(self.height)

    def add_width(self,w):
        self.width = self.width+w
        return(self.width)

    def drawRectangle(self):
        plt.gca().add_patch(plt.Rectangle(0,0), self.height, self.width, fc=self.color)
        plt.axis('scaled')
        plt.show()

bluerectangle = Rectangle(5, 3, 'blue')
#I get the error after I create the object bluerectangle

Tags: theselfaddreturnobjectmatplotlibdef编程
1条回答
网友
1楼 · 发布于 2024-10-04 01:26:28

__init__需要下划线,而不是每边只有一个

所以你需要

class Rectangle(object):
    def __init__(self, height, width, color):
        self.height = height
        self.width = width
        self.color = color
...

因为init方法拼写不正确,所以它没有覆盖object的init方法,这解释了为什么python抱怨object()不带参数

相关问题 更多 >