问题:静态方法-如何从另一个方法调用一个方法?

当我有用于在类中调用另一个方法的常规方法时,我必须这样做

class test:
    def __init__(self):
        pass
    def dosomething(self):
        print "do something"
        self.dosomethingelse()
    def dosomethingelse(self):
        print "do something else"

但是当我有静态方法时,我不能写

self.dosomethingelse()

因为没有实例。在Python中如何从同一个类的另一个静态方法中调用一个静态方法,我该怎么做?

编辑:真是一团糟。好的,我将问题改回原来的问题。我已经有了彼得·汉森(Peter Hansen)评论中第二个问题的答案。如果您认为我应该再提出一个已经存在的答案的问题,请告诉我。

When I have regular methods for calling another method in a class, I have to do this

class test:
    def __init__(self):
        pass
    def dosomething(self):
        print "do something"
        self.dosomethingelse()
    def dosomethingelse(self):
        print "do something else"

but when I have static methods I can’t write

self.dosomethingelse()

because there is no instance. How do I have to do in Python for calling an static method from another static method of the same class?

Edit: what a mess. Ok, I edited back the question to the original question. I already have the answer to the second question that’s in Peter Hansen’s comment. If you think I should open another question for an answer I already have, plz tell me.


回答 0

class.method 应该管用。

class SomeClass:
  @classmethod
  def some_class_method(cls):
    pass

  @staticmethod
  def some_static_method():
    pass

SomeClass.some_class_method()
SomeClass.some_static_method()

class.method should work.

class SomeClass:
  @classmethod
  def some_class_method(cls):
    pass

  @staticmethod
  def some_static_method():
    pass

SomeClass.some_class_method()
SomeClass.some_static_method()

回答 1

在Python中如何从同一个类的另一个静态方法中调用一个静态方法,我该怎么做?

class Test() :
    @staticmethod
    def static_method_to_call()
        pass

    @staticmethod
    def another_static_method() :
        Test.static_method_to_call()

    @classmethod
    def another_class_method(cls) :
        cls.static_method_to_call()

How do I have to do in Python for calling an static method from another static method of the same class?

class Test() :
    @staticmethod
    def static_method_to_call()
        pass

    @staticmethod
    def another_static_method() :
        Test.static_method_to_call()

    @classmethod
    def another_class_method(cls) :
        cls.static_method_to_call()

回答 2

注意-看来问题已经改变了一些。关于如何从静态方法调用实例方法的问题的答案是,您不能不将实例作为参数传入或在静态方法中实例化该实例。

接下来的内容主要是回答“如何从另一个静态方法调用静态方法”:

请记住,Python中的静态方法和类方法之间有区别的。静态方法不使用隐式第一参数,而类方法则将类作为隐式第一参数(通常cls是按惯例)。考虑到这一点,这是您要执行的操作:

如果是静态方法:

test.dosomethingelse()

如果是类方法:

cls.dosomethingelse()

NOTE – it looks like the question has changed some. The answer to the question of how you call an instance method from a static method is that you can’t without passing an instance in as an argument or instantiating that instance inside the static method.

What follows is mostly to answer “how do you call a static method from another static method”:

Bear in mind that there is a difference between static methods and class methods in Python. A static method takes no implicit first argument, while a class method takes the class as the implicit first argument (usually cls by convention). With that in mind, here’s how you would do that:

If it’s a static method:

test.dosomethingelse()

If it’s a class method:

cls.dosomethingelse()

回答 3

好的,类方法和静态方法之间的主要区别是:

  • class方法具有自己的标识,这就是为什么必须在INSTANCE中调用它们的原因。
  • 另一方面,可以在多个实例之间共享静态方法,因此必须从CLASS中调用它

OK the main difference between class methods and static methods is:

  • class method has its own identity, that’s why they have to be called from within an INSTANCE.
  • on the other hand static method can be shared between multiple instances so that it must be called from within THE CLASS

回答 4

您不能从静态方法调用非静态方法,但是可以通过在静态方法内创建实例来实现。

class test2(object):
    def __init__(self):
        pass

    @staticmethod
    def dosomething():
        print "do something"
        #creating an instance to be able to call dosomethingelse(),or you may use any existing instace
        a=test2()
        a.dosomethingelse()

    def dosomethingelse(self):
        print "do something else"

test2.dosomething()

希望对您有帮助:)

you cant call non-static methods from static methods but by creating an instance inside the static method…. it should work like that

class test2(object):
    def __init__(self):
        pass

    @staticmethod
    def dosomething():
        print "do something"
        #creating an instance to be able to call dosomethingelse(),or you may use any existing instace
        a=test2()
        a.dosomethingelse()

    def dosomethingelse(self):
        print "do something else"

test2.dosomething()

hope that will help you :)


回答 5

如果这些不依赖于类或实例,那为什么不仅仅使它们成为一个函数呢?因为这似乎是显而易见的解决方案。当然,除非您认为它将需要被覆盖,子类化等等。如果是这样,那么前面的答案是最好的选择。我只是简单地提供一个可能满足或可能不适合某人需求的替代解决方案而感到沮丧;)。

因为正确的答案将取决于相关代码的用例;)享受

If these don’t depend on the class or instance then why not just make them a function? As this would seem like the obvious solution. Unless of course you think it’s going to need to be overwritten, subclass etc. If so then the previous answers are the best bet. Fingers crossed I wont get marked down for merely offering an alternative solution that may or may not fit someones needs ;).

As the correct answer will depend on the use case of the code in question ;) Enjoy


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。