Detailed TypeError error resolution in Python

Source: Internet
Author: User
Beginners in the learning of Python, will encounter a lot of pits, below to specifically say one of them.

When writing object-oriented programs using Python, newbies may encounter Typeerror:this constructor takes no arguments this error.

For example, the following program:

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", myball.direction
Print "Now I ' m going to bounce the ball"
Print
Myball.bounce ()
Print "Now the ball ' s direction is", myball.direction

Operation will be error:

======================= restart:h:\python\bounce1.py =======================

Traceback (most recent):
File "H:\python\bounce1.py", line one, in <module>
Myball=ball ("Red", "small", "down")
Typeerror:this constructor takes no arguments

The reason for the error is that in Python the constructor writing format is __init__, not _init_, which is double-underlined on both sides of the INIT, not single-underlined.

The following changes are followed:

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", myball.direction
Print "Now I ' m going to bounce the ball"
Print
Myball.bounce ()
Print "Now the ball ' s direction is", myball.direction

This is the correct result for the operation:

======================= restart:h:\python\bounce1.py =======================
I just created a ball.
My ball was small
my ball was red
my ball's direction is off
now I ' m going to bounce the ball

now the ball ' S direction is up

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.