+ 3
static class methods in python?
I know python have instance methods only , but i was coding and i came across a problem so , i was trying out random solutions . And i tried this code just to see what would happen . ################################ class person(): firs_name = "jay" last_name = "aljoe" def info(): print(person.firs_name) print(person.last_name) person.info() ########################### And it worked somehow . Now im confused as f . Does python have class methods ?
1 Answer
+ 5
Yes, python does have static and class methods.
You can designate them by using the decorators:
@classmethod
and
@staticmethod
an instance method should have self as its first parameter
a class method will need cls passed as its first parameter
a static method does not need self or cls as a first parameter
https://realpython.com/blog/python/instance-class-and-static-methods-demystified/