对象和类的Python脚本未运行
我对14号线和15号线有问题 我尝试创建一个名为“Person”的类的对象,但当我运行脚本时,它告诉我AttributeError对象和类的Python脚本未运行,python,class,object,Python,Class,Object,我对14号线和15号线有问题 我尝试创建一个名为“Person”的类的对象,但当我运行脚本时,它告诉我AttributeError class Person: def __init__(self, name, weight, complexion, hobby): self.name = name self.weight = weight self.complexion = complexion self.hobby = h
class Person:
def __init__(self, name, weight, complexion, hobby):
self.name = name
self.weight = weight
self.complexion = complexion
self.hobby = hobby
def introduce_self(self):
print("My name is" ,self.name, self.weight , "in weight," ,self.complexion,"in complexion," , "and I do" ,self.hobby, "for relaxation")
p1 = ("Shereden", 30, "fair", "watching Cumcumbagyea")
p2 = ("Stephen", 40, "dark", "surfing the net")
p1.introduce_self()
p2.introduce_self()
回溯(最近一次呼叫最后一次):
文件“C:/Users/HP/PycharmProjects/Opeemu/Hello_world.py”,第14行,在
p1.介绍_self()
AttributeError:“set”对象没有属性“Introduct\u self”
您将p1
和p2
定义为set
而不是Person
对象
你想要的是:
p1 = Person("Shereden", 30, "fair", "watching Cumcumbagyea")
p2 = Person("Stephen", 40, "dark", "surfing the net")
另外,请确保缩进对于“介绍自我”方法是正确的,在使用任何对象方法之前,您需要先创建对象
p1 = ("Shereden", 30, "fair", "watching Cumcumbagyea")
p2 = ("Stephen", 40, "dark", "surfing the net")
此外,init函数应为:
def __init__(self, name, weight, complexion, hobby): ...
否则,它将是另一个您可以调用的方法,如p1.init()请尝试以人类可读的格式编写问题。另外,它是
def\uuuu init\uuuu(self…):…
def __init__(self, name, weight, complexion, hobby): ...