在Python中从静态方法调用非静态方法 [英] Calling non-static method from static one in Python

查看:739
本文介绍了在Python中从静态方法调用非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到在Python中从静态方法中调用非静态方法的可能性.

I can't find if it's possible to call a non-static method from a static one in Python.

谢谢

好的.静态与静态又如何呢?我可以这样做吗?

Ok. And what about static from static? Can I do this:

class MyClass(object):

    @staticmethod
    def static_method_one(cmd):
    ...

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

推荐答案

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

It's perfectly possible, but not very meaningful. Ponder the following class:

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)

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

Obviously, we can call this in the expected ways:

>>> 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)的快捷方式".这就是为什么方法中要传递自我"参数的原因.自我这个名字不是神奇的,可以随便叫它.

The syntax instance.normal_method() is pretty much just a "shortcut" for MyClass.normal_method(instance). That's why there is this "self" parameter in methods, to pass in self. The name self is not magical, you can call it whatever you want.

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

The same trick is perfectly possible from withing a static method. You can call the normal method with an instance as first parameter, like so:

    @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!

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

So the answer is yes, you can cal non-static methods from static methods. But only if you can pass in an instance as first parameter. So you either have to generate it from inside the static method (and in that case you are probably better off with a class method) or pass it in. But if you pass in the instance, you can typically just make it a normal method.

您可以,但是,这毫无意义.

So you can, but, it's pretty pointless.

然后提出一个问题:为什么要这么做?

And that then begs the question: Why do you want to?

这篇关于在Python中从静态方法调用非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆