程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python error (2): takes 0 positive arguments but 1 was given

編輯:Python

I'm learning python Class time of , Try running the following code :

# Create a dog class
class Dog():
""" A simple attempt to simulate a puppy """
def __init__(self,name,age):
""" Initialization property namez and age"""
self.name = name
self.age = age
def hungry():
""" Print a message """
print("The dog is hungry")
# Create an object
my_dog = Dog("Dalin",4)
# Calling method
my_dog.hungry()

appear Report errors :


Finally found The mistake is :

among hungry() Method must have a parameter , The general default is "self"

The revised code is :

# Create a dog class
class Dog():
""" A simple attempt to simulate a puppy """
def __init__(self,name,age):
""" Initialization property namez and age"""
self.name = name
self.age = age
def hungry(self):
""" Print a message """
print("The dog is hungry")
# Create an object
my_dog = Dog("Dalin",4)
# Calling method
my_dog.hungry()

summary :

When defining a method in a class , At least one parameter should be defined , It is usually named "self", Instance of its representative class , Represents the address of the current object , It's a must , Even if you do not need to pass in the corresponding parameters when calling .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved