Home >Backend Development >Python Tutorial >Detailed explanation of TypeError error solutions in python
Novices will encounter many pitfalls when learning python. Let’s talk about one of them in detail below.
When writing object-oriented programs in python, novices may encounter the error TypeError: this constructor takes no arguments.
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"
## This 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
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
This is the correct running result:
==================== ==== 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
The above is the detailed content of Detailed explanation of TypeError error solutions in python. For more information, please follow other related articles on the PHP Chinese website!