【问题标题】:How do I call a classes method from another class without initialising the first class?如何在不初始化第一个类的情况下从另一个类调用类方法?
【发布时间】:2018-12-19 16:51:02
【问题描述】:

我有类似以下的内容:

class Class1(object):
    def __init__(self):
        print('I've init-ed')
    def print_me(self, string):
        print(string)

class Class2(object):
    def __init__(self):
        print('Class 2 init')
    def print_me_from_other_class(self, string):
        Class1().print_me(string)

然后如果我调用类似的东西:

test = Class2()
test.print_me_from_other_class('TEST')

然后我得到:

Class 2 init
I've init-ed
TEST

我想做的是从 Class2 中的方法调用 Class1 中的方法,但不从类 1 中调用 init。所以我最终会得到:

Class 2 init
TEST

我尝试在 print_me_from_another_class(self, string) 函数中删除 Class1 之后的括号,所以它说:

print_me_from_another_class(self, string):
    Class1.print_me(string)

但这会引发错误:

TypeError: print_me() missing 1 required positional argument: 'string'

有什么想法吗?或者这是一种不好的处理方式?似乎它会节省重写代码所以应该是一件好事。

编辑:

我已经确定我需要将 Class2 的实例传递给函数,以便 Class2 中的函数变为:

def print_me_from_other_class(self, string):
    Class1.print_me(self, string)

而且它有效!但是我仍然想知道这是否是一种很好的做事方式?

【问题讨论】:

  • 如果你想使用没有实例的实例方法,那么它不应该是实例方法。
  • What the others say and self.method() 只是 klass.method(self) 的缩写,它允许您在 Class2 中调用 Class1.print_me(self, string)

标签: python python-3.x class inheritance


【解决方案1】:

你试过@staticmethod 吗?类似:

class C1:
 def __init__(self):
  print "C1 INIT"
 def f(self):
  C2.c()

class C2:
 def __init__(self):
  print "C2 INIT"
 @staticmethod
 def c():
  print "method c from C2"

@staticmethod 装饰器用于制作静态方法,通常这种方法在你想调用一个不初始化类的方法时很有用。是组织功能的好方法。

【讨论】:

    【解决方案2】:

    注意 print_me 中的“self”参数 - 这意味着函数 必须 在 Class1 的特定实例上调用。你想要的是一个静态函数。下面的代码演示了这一点。

    class Class1(object):
        def __init__(self):
            print("I've init-ed")
        @staticmethod
        def print_me(string):
            print(string)
    
    class Class2(object):
        def __init__(self):
            print('Class 2 init')
        def print_me_from_other_class(self, string):
            Class1.print_me(string)
    
    
    test = Class2()
    test.print_me_from_other_class('TEST')
    

    输出:

    Class 2 init
    TEST
    

    【讨论】:

    • 它不必在 class1 的实例上调用。
    • @juanpa.arrivillaga 如果不出现 TypeError,你会怎么做?
    • 正如操作所展示的那样。 Class1.class_one_method(class_two_instance)
    • @juanpa.arrivillaga 仅在 Class2 继承自 Class1 时才有效。否则你会得到“TypeError: unbound method print_me2() must be called with Class1 instance as first argument (got Class2 instance)”。
    • 啊,你使用的是 Python 2。这不再适用,没有更多未绑定的方法,它只是一个普通函数
    【解决方案3】:

    您的代码块的问题是Class1.print_me(string) 创建了新对象,这就是为什么调用 init 方法来避免这种情况。简单的解决方案是使用继承。

    class Class1(object):
        def __init__(self):
            print('Ive init-ed')
        def print_me(self, string):
            print(string)
    
    class Class2(Class1):
        def __init__(self):
            print('Class 2 init')
        def print_me_from_other_class(self, string):
            self.print_me(string)
    obj=Class2()
    obj2.print_me_from_other_class("hi")
    

    你也可以直接调用基类函数 obj2.print_me("hello")

    【讨论】:

      【解决方案4】:

      解决方案

      感谢大家的帮助,我现在对它有了更好的理解。我最终按照 cmets 中的建议所做的是从类中删除这些特定方法并使它们成为全局函数。实际上,我经历并删除了所有没有专门引用 self 的方法(或者将它们保留为类方法是有意义的)并使它们成为全局的。现在看它,我认为这是构建代码的更好方法。

      谢谢大家。

      【讨论】:

        猜你喜欢
        • 2012-07-17
        • 2022-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-20
        • 1970-01-01
        • 2014-02-07
        相关资源
        最近更新 更多