2

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';
  }
}
1
  • 1
    @MisterMiyagi Most people unfamiliar with Python use "class method" to refer to a method belonging to a class (regardless of its type), rather than a classmethod specifically. Commented Dec 14, 2021 at 16:33

1 Answer 1

4

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

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

Class.method()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.