簡體   English   中英

python 調用 class 方法而不初始化 class

[英]python calling a class method without initializing the class

I want to call a function from a python class but it would be necessary to initialize it so is there a way to call class methods without initializing the class. 我有一個js背景我想要的和這個類似

class foo {
  static calculate() {
    return 'bar';
  }
}

您可以使用@staticmethod裝飾器在 python 中定義 static 方法:

class Class:
    @staticmethod
    def method():
        print("Method called")

Class.method()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM