About 51 results
Open links in new tab
  1. python - Meaning of @classmethod and @staticmethod for a beginner ...

    Here we have __init__, a typical initializer of Python class instances, which receives arguments as a typical instance method, having the first non-optional argument (self) that holds a reference to a …

  2. What is the difference between @staticmethod and @classmethod in …

    Sep 26, 2008 · What is the difference between a method decorated with @staticmethod and one decorated with @classmethod?

  3. python - What is the purpose of class methods? - Stack Overflow

    The Python class method is just a decorated function and you can use the same techniques to create your own decorators. A decorated method wraps the real method (in the case of @classmethod it …

  4. python - Class (static) variables and methods - Stack Overflow

    Sep 16, 2008 · See what the Python tutorial has to say on the subject of classes and class objects. @Steve Johnson has already answered regarding static methods, also documented under "Built-in …

  5. python - Difference between class and instance methods - Stack …

    Jun 16, 2013 · Class methods The idea of a class method is very similar to an instance method, only difference being that instead of passing the instance hiddenly as a first parameter, we're now passing …

  6. What is the purpose of the `self` parameter? Why is it needed?

    813 The reason you need to use self is because Python does not use special syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method …

  7. class method - what is the use and when to use @classmethod in …

    Jan 19, 2019 · 2) Class method (create_with_birth_year and print_species) need no instance and use cls to access the class and influence the state of a class. We can use @classmethod to make a …

  8. How do I type hint a method with the type of the enclosing class?

    Python 3.14+: It'll just work Python 3.14 has a new, lazily evaluated annotation implementation specified by PEP 749 and 649. Annotations will be compiled to special __annotate__ functions, executed …

  9. python - How to print instances of a class using print ... - Stack Overflow

    A simple decorator @add_objprint will help you add the __str__ method to your class and you can use print for the instance. Of course if you like, you can also use objprint function from the library to print …

  10. Static methods in Python? - Stack Overflow

    Apr 10, 2009 · class MyClass(object): def the_static_method(x): print(x) the_static_method = staticmethod(the_static_method) MyClass.the_static_method(2) # outputs 2 This is entirely identical …