python静态方法怎么调用_在Python中从静态方法调用非静态方法

1586010002-jmsa.png

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

Thanks

EDIT:

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!

But also consider this:

>>> MyClass.normal_method(instance, "Success!")

Normal method called with instance <__main__.MyClass instance at 0xb7d26bcc> and data Success!

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值