首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >继承类Python中init方法中的参数

继承类Python中init方法中的参数
EN

Stack Overflow用户
提问于 2021-12-06 22:27:19
回答 1查看 59关注 0票数 0

我是Python的初学者,我正在努力理解课程。在这种情况下,我不知道为什么不能从init方法中获取参数“权重”?

代码语言:javascript
复制
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)

它提出:

代码语言:javascript
复制
      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'
EN

回答 1

Stack Overflow用户

发布于 2021-12-06 23:35:48

您只需创建对象,然后调用该方法:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70252727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档