0
What is a class method?
4 Answers
+ 2
It is a function that is defined inside a class.
+ 1
A class method belongs to the class itself unlike a normal method which belongs to an instance of the class.
You don't have to instantiate an instance of the class via a constructor and then call a method when using a class method. Instead you just call <Class>.method
They can be used for utilities or factory class methods as oppsed to normal methods that typically perform action on the properties of sn instance of a class.
0
tnx
- 2
it is a function into the class with self as argument.
you can call the function when you create an object with the class
class Dog:
def __init__(self,name):
self.name = name
def bark(self):
print(self.name,"says Woof")
>>> fido = Dog("Fido")
>>> fido.bark()
"Fido says Woof"