我是Python的初学者,我正在努力理解课程。在这种情况下,我不知道为什么不能从init方法中获取参数“权重”?
class Person:
def __init__(self, weight):
self.weight=weight
def go_in(self, age,height):
enter_to_disco= age*height*self.weight
if enter_to_disco<10:
print('enter to disco')
else:
print(f'do not enter to disco')
peter=Person.go_in(13,20,1)它提出:
7 def go_in(self, age,height):
8
----> 9 enter_to_disco= age*height*self.weight
10 if enter_to_disco<10:
11 print('enter to disco')
AttributeError: 'int' object has no attribute 'weight'发布于 2021-12-06 23:35:48
您只需创建对象,然后调用该方法:
class Person:
def __init__(self, weight):
self.weight=weight
def go_in(self, age,height):
enter_to_disco= age*height*self.weight
if enter_to_disco<10:
print('enter to disco')
else:
print(f'do not enter to disco')
Person(90).go_in(20,1)https://stackoverflow.com/questions/70252727
复制相似问题