Home > OS >  python calling a class method without initializing the class
python calling a class method without initializing the class

Time:12-15

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';
  }
}

CodePudding user response:

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

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

Class.method()
  • Related