2009-09-06 194 views
4

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

感谢

编辑: 确定。那静态静态呢?我可以这样做:

class MyClass(object): 

    @staticmethod 
    def static_method_one(cmd): 
    ... 

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

回答

2

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

+0

Ned Batchelder @怎么样从静态调用静态?语法是什么?我得到了“NameError:全局名称'my_static_method'未定义”到目前为止。 – legesh 2009-09-06 12:30:46

+0

与所有其他方法相同的语法:object_or_class。方法(参数) – 2009-09-06 13:22:46

0

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

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从空闲粘贴)[**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 
+0

第二个例子应该调用'cls.class_method_two(x)';这也表明为什么OP应该使用'classmethod'而不是'staticmethod'。 – u0b34a0f6ae 2009-09-06 13:04:58

3

使用类的方法,而不是静态方法。为什么要把它放在课堂上?

class MyClass(object): 

    @classmethod 
    def static_method_one(cls, cmd): 
    ... 

    @classmethod 
    def static_method_two(cls, cmd): 
     cls.static_method_one(cmd) 
9

这是完全可能的,但不是很有意义。思考以下类:

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)。这就是为什么在方法中存在这个“自我”参数来传递自我的原因。名字自我并不神奇,你可以随心所欲地称呼它。

使用静态方法完全可以实现同样的技巧。您可以致电与实例的正常方法,第一个参数,像这样:

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

因此,答案是肯定的,你可以从CAL静态方法非静态方法。但前提是你可以传入一个实例作为第一个参数。所以你必须从静态方法内部生成它(在这种情况下,你最好用类方法)或者传递它。但是如果你传入实例,通常可以将它作为一个常规方法。

所以你可以,但是,这是非常没有意义的。

然后,这引出了一个问题:为什么你想?