staticmethod makes my brain hurt

Ethan Furman ethan at stoneleaf.us
Wed Nov 16 22:24:02 EST 2011


Roy Smith wrote:
> class User(Document):
>     @staticmethod
>     def _get_next_id():
>       [blah, blah, blah]  
>       return id
> 
>     user_id = IntField(required=True, default=_get_next_id)

If you don't call '_get_next_id()' from any class methods (in other 
words, if you don't need to ever say 'self._gen_next_id()') then you can 
remove the '@staticmethod':

     def _get_next_id():
       [blah, blah, blah]
       return id

     user_id = IntField(required=True, default=_get_next_id)

If you do need to sometimes call it from a method then still leave off 
the '@staticmethod', and give 'self' a default of 'None':

     def _get_next_id(self=None):
       [blah, blah, blah]
       return id

     user_id = IntField(required=True, default=_get_next_id)

~Ethan~



More information about the Python-list mailing list