python中typeerror_详解python中的TypeError错误解决办法

新手在学习python时候,会遇到很多的坑,下面来具体说说其中一个。

在使用python编写面向对象的程序时,新手可能遇到TypeError: this constructor takes no arguments这个错误。

例如下面的程序:

class Ball:

def _init_(self,color,size,direction):

self.color=color

self.size=size

self.direction=direction

def bounce(self):

if self.direction=="down":

self.direction="up"

myBall=Ball("red","small","down")

print "I just created a ball."

print "My ball is",myBall.size

print "My ball is",myBall.color

print "My ball's direction is",myBall.direction

print "Now I'm going to bounce the ball"

print

myBall.bounce()

print "Now the ball's direction is",myBall.direction

运行会报错:

======================= RESTART: H:\python\bounce1.py =======================

Traceback (most recent call last):

File "H:\python\bounce1.py", line 11, in

myBall=Ball("red","small","down")

TypeError: this constructor takes no arguments

出错原因是,在python中构造函数书写格式是__init__,而不是_init_,即在init两侧都是双下划线,不是单下划线。

修改后如下:

class Ball:

def __init__(self,color,size,direction):

self.color=color

self.size=size

self.direction=direction

def bounce(self):

if self.direction=="down":

self.direction="up"

myBall=Ball("red","small","down")

print "I just created a ball."

print "My ball is",myBall.size

print "My ball is",myBall.color

print "My ball's direction is",myBall.direction

print "Now I'm going to bounce the ball"

print

myBall.bounce()

print "Now the ball's direction is",myBall.direction

这是正确的运行结果:

======================= RESTART: H:\python\bounce1.py =======================

I just created a ball.

My ball is small

My ball is red

My ball's direction is down

Now I'm going to bounce the ball

Now the ball's direction is up

以上就是详解python中的TypeError错误解决办法的详细内容,更多请关注Gxl网其它相关文章!

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

TAG标签:python

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在你提到的错误信息,出现了一个关于TypeError的报错,报错信息为"TypeError: set_auto_reply() takes 1 positional argument but 65 were given"。这意味着set_auto_reply()函数只接受一个参数,但是却传入了65个参数。根据错误信息并没有提供具体的代码,所以很难准确地判断问题所在。 根据你提供的引用内容,我没有找到与这个具体报错相关的解释。因此,要解决这个问题,我们需要检查你的代码,特别是set_auto_reply()函数的调用部分。请确保你在调用set_auto_reply()函数时只传递一个参数。 另外,在Python,这个报错也可能是因为你传递的参数类型与函数所期望的参数类型不匹配。请检查传递给set_auto_reply()函数的参数是否正确,并且符合函数所期望的参数类型。 如果你能提供更多的代码和上下文信息,我将能够给出更准确的解决方案。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [django报错:add_atr() takes 0 positional arguments but 1 was given](https://download.csdn.net/download/weixin_38531788/13753788)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python 详解](https://blog.csdn.net/blue0bird/article/details/78808747)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值