Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15803

Re: staticmethod makes my brain hurt

Date 2011-11-16 19:24 -0800
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: staticmethod makes my brain hurt
References <roy-DD1C5B.21305716112011@news.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.2790.1321501935.27778.python-list@python.org> (permalink)

Show all headers | View raw


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~

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

staticmethod makes my brain hurt Roy Smith <roy@panix.com> - 2011-11-16 21:30 -0500
  Re: staticmethod makes my brain hurt alex23 <wuwei23@gmail.com> - 2011-11-16 18:44 -0800
    Re: staticmethod makes my brain hurt Roy Smith <roy@panix.com> - 2011-11-16 21:52 -0500
  Re: staticmethod makes my brain hurt Ethan Furman <ethan@stoneleaf.us> - 2011-11-16 19:24 -0800
    Re: staticmethod makes my brain hurt alex23 <wuwei23@gmail.com> - 2011-11-17 18:31 -0800
  Re: staticmethod makes my brain hurt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-17 04:43 +0000
    Re: staticmethod makes my brain hurt Roy Smith <roy@panix.com> - 2011-11-17 00:26 -0500
    Re: staticmethod makes my brain hurt Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-11-17 01:20 -0500
  Re: staticmethod makes my brain hurt Dotan Cohen <dotancohen@gmail.com> - 2011-11-17 08:44 +0200
  Re: staticmethod makes my brain hurt Ian Kelly <ian.g.kelly@gmail.com> - 2011-11-17 00:37 -0700
  Re: staticmethod makes my brain hurt Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-11-17 10:17 +0100
  Re: staticmethod makes my brain hurt Dotan Cohen <dotancohen@gmail.com> - 2011-11-17 13:13 +0200
  Re: staticmethod makes my brain hurt Chris Angelico <rosuav@gmail.com> - 2011-11-17 22:38 +1100

csiph-web