@staticmethod or def function()?

Santiago Basulto santiago.basulto at gmail.com
Wed Oct 31 12:19:28 EDT 2018

I usually recommend my students to use @classmethod. It's a consistent API
with a regular instance method, and receiving the class as first parameter
(usually `cls`) is useful if you have to extend your classes.

About separating a staticmethod in a regular function, it's mostly a matter
of taste and organization. It's hard to say. Your intuition is good though,
if you need to use the functionality by several classes, then it's probably
not a staticmethod but an external function.

On Wed, Oct 31, 2018 at 12:58 PM Tobiah <toby at tobiah.org> wrote:

> My IDE (pycharm) suggests that I mark my class methods
> with @staticmethod when they don't use 'self'.  Sounds
> good.  I did that then it suggested I had the option
> to make a regular function instead, at the file level.
> That's a possibility.  I was thinking that I'd leave
> the method in the class unless it was ever also used
> by another class.  What do you think?
> --
> https://mail.python.org/mailman/listinfo/python-list

Santiago Basulto.-

More information about the Python-list mailing list