3 ms·
One thing I never understood from it is the recommendation to avoid staticmethods, classmethods[1]. I was befuddled by it at first, looked it up on moma, and ev
by woadwarrior01 4y ago
One thing I never understood from it is the recommendation to avoid staticmethods, classmethods[1]. I was befuddled by it at first, looked it up on moma, and even asked around, but never got a convincing answer during my time there. IIRC, their C++ style guide had even stronger opinions, diverging from the norm.
[1]: https://google.github.io/styleguide/pyguide.html#217-function-and-method-decorators https://google.github.io/styleguide/pyguide.html#217-functio...
- Kwpolska 4y ago@staticmethod is kinda pointless in two ways. One, it has the same effect as @classmethod, except the first argument, so you can always just use @classmethod. Two, you could argue that static methods don’t make much sense in a language which has top-level functions (unlike Java). If something is part of the class but does not make use of the class state and should be usable from outside of the class, then it should be a plain function, not a @staticmethod. (Overzealous linters that suggest @staticmethod when the method could be a function, or when the method is part of some public interface and does not necessarily have to be static in other implementations, are dumb.)
- rolisz 4y agoI think the argument is that static methods are just functions, they are not strongly tied to a class or it's objects, so then let's not have two kinds of entities (normal functions and static methods) that do the same thing. Class methods are a weird thing that can be used to affect all objects of a class, so are kinda global in a sense, which is why they should be avoided.
- peteradio 4y agostatic methods are nice for clarifying scope
- gorgoiler 4y agoTo add to the sibling comments, nudging you into module level constructors for classes will encourage more modularity in general. If you have a class like this with a separate constructor in the same scope as the class… class Cow: def __init__(self, name) … def random_cow() -> Cow: return Cow(uuid()) …you are more likely to roll this all up into farm.cow than you are to lump all the animals together in a single farm module. Modularity is nice of course because it helps you step away from implementation detail (close the file, forget about how it works, and just use it) and your code gets split up into little pieces that helps your e.g. bazel monorepo build/test work efficiently.