In python, during a recent code review, it was pointed out that using classmethods and staticmethods are considered bad practice because they enlarge the scope of the given class.
In the specific case, it was suggested that a factory method could become a classmethod as an alternative constructor:
class FooConfig:
...
class Foo:
def __init__(self, x: int):
self.x = x
def create_foo(c: FooConfig):
f = Foo(c.x)
return f
could become
class FooConfig:
...
class Foo:
def __init__(self, x: int):
self.x = x
@classmethod
def from_config(cls, c: FooConfig):
return cls(c.x)
and senior members rejected it saying that classmethods and staticmethods are bad practice because they enlarge the scope of the class. I’d like to understand if that is indeed the case.
EDIT: The question is closed because it seems to be opinion based. I’d like to ask then under what conditions are classmethods bad practice? What would be appropriate examples of classmethods being bad practice?
The re-formulation also asked about staticmethods. This question has been answered here: Is using a lot of static methods a bad thing?. I’d like to see a specific example or general guidelines, like in this answer, for classmethods.
Hsflores is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3