【问题标题】:How to call function in class in Python如何在 Python 中调用类中的函数
【发布时间】:2023-01-05 18:50:22
【问题描述】:

我的问题是,如何在类或父类中调用函数?

例如:

class Contact:
    
    def __init__(self):
        self.phone_number = 0976

    def func1(self):
        '''do something'''
    
    def func2(self):
        '''do something'''

    def func3(self):
        '''do something'''
        '''call func1'''
    
    def func4():
        '''call func3'''
        '''call func2'''

我尝试了正常的函数调用,但没有用。 与父类相同。

我在谷歌搜索过,但作为菜鸟(新程序员),我找到了一些建议,但没有用。我确实可能没有很好地理解它。

【问题讨论】:

  • 如果您尝试了"but it didn't work",您应该将其包含在您的问题文本中,以便我们准确了解您的想法。

标签: python function class methods


【解决方案1】:

您需要先初始化您的类的对象,然后调用 Python 的函数来调用方法

contact = Contact()
contact.func1() 
contact.func2() 
contact.func3() 
contact.func4() 

【讨论】:

  • 请不要推测 OP 的实际含义。
  • contact.func1()..
  • 抱歉输入错误@luk2302
  • 我不认为这是 OP 的意思,所以我要求澄清。
猜你喜欢
  • 2019-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多