简体   繁体   中英

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. I have a js background I what I want is similar to this

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

You can use @staticmethod decorator to define static methods in python:

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

Class.method()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM