Class methods should take cls
as the first argument.
The @classmethod
decorator converts the method to a class method.
When the class method is called, Python will implicitly pass in the class as the first argument, just like how an instance method receives the instance (self
, by convention).
While it is true that the first argument could be called anything, Python should be written to be read, and Python developers expect the first argument of class methods to be named cls
.
If our GitHub code review bot spots this issue in your pull request it gives this advice:
1 | + | class FooBarClass: | |
2 | + | @classmethod | |
3 | + | def from_dict(self): |
Class methods should take cls
as the first argument.
- | def from_dict(self): |
+ | def from_dict(cls): |
4 | + | pass |