Answers for "staticmethod vs classmethod python"

13

static methods in python

class Calculator:

    # create addNumbers static method
    @staticmethod
    def addNumbers(x, y):
        return x + y

print('Product:', Calculator.addNumbers(15, 110))
Posted by: Guest on June-08-2020
3

static methods in python

class Calculator:

    def addNumbers(x, y):
        return x + y

# create addNumbers static method
Calculator.addNumbers = staticmethod(Calculator.addNumbers)

print('Product:', Calculator.addNumbers(15, 110))
Posted by: Guest on June-08-2020
0

staticmethod vs classmethod python

# With classmethods, the class of the object instance is
# implicitly passed as the first argument instead of self.

class A(object):
    def foo(self, x):
        print(f"executing foo({self}, {x})")

    @classmethod
    def class_foo(cls, x):
        print(f"executing class_foo({cls}, {x})")

    @staticmethod
    def static_foo(x):
        print(f"executing static_foo({x})")

a = A()
Posted by: Guest on August-21-2021

Python Answers by Framework

Browse Popular Code Answers by Language