Hacker News new | ask | show | jobs
by dcferreira 1120 days ago
Cool post, I've also been using more and more type hints and it really makes writing python more enjoyable. Although unfortunately it's still quite common for libraries to not have type stubs.

On the section of construction functions, I've always used/seen `@classmethod` instead of `@staticmethod`. I just had a quick look at some big python libraries (pandas, transformers), and they do use class methods. Is there any particular reason you'd want to use static methods instead?

3 comments

Factory methods should be class methods if the class is meant to be inherited from. Since most classes people write day to day aren’t going to be inherited from, ever, the distinction is irrelevant for the most part. I doubt they have a particular reason for choosing the less extensible option.
FWIW personally I use @staticmethod when I have small-ish "utility" methods that are only ever used in-class. Basically just a convenient way of organizing code and making the intended context clear.
I don't think that it's a good idea in general to inherit the construction functions. But if you wanted to do that, classmethod is the way to go, of course.