【问题标题】:Calling non-static method from static one in Python在 Python 中从静态方法调用非静态方法
【发布时间】:2009-09-06 12:20:29
【问题描述】:

我找不到是否可以从 Python 中的静态方法调用非静态方法。

谢谢

编辑: 行。那么静态中的静态呢?我可以这样做吗:

class MyClass(object):

    @staticmethod
    def static_method_one(cmd):
    ...

    @staticmethod
    def static_method_two(cmd):
        static_method_one(cmd)

【问题讨论】:

  • 当然你可以调用另一个静态方法。我的意思是,当您不能从静态调用非静态时,您必须这样做。

标签: python oop


【解决方案1】:

完全有可能,但意义不大。思考以下课程:

class MyClass:
    # Normal method:
    def normal_method(self, data):
        print "Normal method called with instance %s and data %s" % (self, data)

    @classmethod
    def class_method(cls, data):
        print "Class method called with class %s and data %s" % (cls, data)

    @staticmethod
    def static_method(data):
        print "Static method called with data %s" % (data)

显然,我们可以按预期的方式调用它:

>>> instance = MyClass()
>>> instance.normal_method("Success!")
Normal method called with instance <__main__.MyClass instance at 0xb7d26bcc> and data Success!

>>> instance.class_method("Success!")
Class method called with class __main__.MyClass and data Success!

>>> instance.static_method("Success!")
Static method called with data Success!

但也要考虑一下:

>>> MyClass.normal_method(instance, "Success!")
Normal method called with instance <__main__.MyClass instance at 0xb7d26bcc> and data Success!

语法instance.normal_method() 几乎只是MyClass.normal_method(instance) 的“快捷方式”。这就是为什么在方法中有这个“self”参数,用来传入self。 self这个名字并不神奇,你可以随便叫它。

使用静态方法完全可以实现相同的技巧。您可以使用实例作为第一个参数调用普通方法,如下所示:

    @staticmethod
    def a_cool_static_method(instance, data):
        print "Cool method called with instance %s and data %s" % (instance, data)
        MyClass.normal_method(instance, data)
        MyClass.class_method(data)
        MyClass.static_method(data)

>>> instance.a_cool_static_method(instance, "So Cool!")
Cool method called with instance <__main__.MyClass instance at 0xb7d26bcc> and data So Cool!
Normal method called with instance <__main__.MyClass instance at 0xb7d26bcc> and data So Cool!
Class method called with class __main__.MyClass and data So Cool!
Static method called with data So Cool!

所以答案是肯定的,你可以从静态方法中调用非静态方法。但前提是您可以将实例作为第一个参数传递。因此,您要么必须从静态方法内部生成它(在这种情况下,使用类方法可能会更好)或将其传入。但如果传入实例,通常可以将其设为普通方法。

所以你可以,但是,这是毫无意义的。

这就引出了一个问题:你为什么要这样做?

【讨论】:

    【解决方案2】:

    在其他答案和您的后续问题之后 - 关于静态方法中的静态方法:是的,您可以:

    >>> class MyClass(object):
        @staticmethod
        def static_method_one(x):
            return MyClass.static_method_two(x)
        @staticmethod
        def static_method_two(x):
            return 2 * x
    
    
    >>> MyClass.static_method_one(5)
    10
    

    而且,如果你好奇的话,对于类方法中的类方法也是可以的(很容易在解释器中测试这些东西 - 所有这些都是从 2.5.2 中的 Idle 剪切和粘贴的)[**EDITED to make correction in usage pointed out by others**]

    >>> class MyClass2(object):
        @classmethod
        def class_method_one(cls, x):
            return cls.class_method_two(x)
        @classmethod
        def class_method_two(cls, x):
            return 2 * x
    
    
    >>> MyClass2.class_method_one(5)
    10
    

    【讨论】:

    • 第二个例子应该调用cls.class_method_two(x);这也说明了为什么 OP 应该使用 classmethod,而不是 staticmethod
    • @u0b34a0f6ae 取决于他们想做什么。如果他们想传入正确的子类,那么类方法是合适的。如果他们总是想使用MyClass,那么传递子类就没有什么意义了(就像类方法那样)。
    【解决方案3】:

    使用类方法,而不是静态方法。为什么还要把它放在一个类中?

    class MyClass(object):
    
        @classmethod
        def static_method_one(cls, cmd):
        ...
    
        @classmethod
        def static_method_two(cls, cmd):
            cls.static_method_one(cmd)
    

    【讨论】:

    • 封装?
    【解决方案4】:

    在静态方法中,您没有 self 实例:您在哪个对象上调用非静态方法?当然,如果你有一个实例,你可以调用它的方法。

    【讨论】:

    • Ned Batchelder@ 从静态调用静态怎么样?语法是什么?到目前为止,我收到“NameError:未定义全局名称'my_static_method'”。
    • 与所有其他方法的语法相同:object_or_class.method(parameters)
    【解决方案5】:

    没有类的实例是不可能的。您可以在方法f(x, y, ..., me) 中添加一个参数,并使用me 作为对象来调用非静态方法。

    【讨论】:

    • 有可能。阅读以上答案。只是做起来并不简单,但你可以做到。 MyClass.normal_method(实例,数据)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多