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


Groups > comp.lang.python > #50507

Re: Callable or not callable, that is the question!

From Duncan Booth <duncan.booth@invalid.invalid>
Newsgroups comp.lang.python
Subject Re: Callable or not callable, that is the question!
Date 2013-07-12 07:36 +0000
Message-ID <XnsA1FB578CDBB3Cduncanbooth@127.0.0.1> (permalink)
References <n6n2ba-ubg.ln1@satorlaser.homedns.org> <mailman.4586.1373551911.3114.python-list@python.org> <11l4ba-u4l.ln1@satorlaser.homedns.org>

Show all headers | View raw


Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> wrote:

> Am 11.07.2013 16:11, schrieb Peter Otten:
>> Ulrich Eckhardt wrote:
>>> Bug or feature?
>>
>> No bug. Missing feature if you come up with a convincing use-case.
> 
> class Parser:
>      def _handle_bool(input):
>          # ...
>          pass
> 
>      types = {'bool': _handle_bool,
>               'boolean': _handle_bool,}
> 
>      def parse(self, line):
>          t,s,v = line.partition(':')
>          handler = types[t]
>          return handler(v)
> 
> I want a utility function that really behaves just like a function. I'd 
> prefer to nest it inside the class that uses it, to make the code easier 
> to understand. Since I don't want the implicit self-binding either, I 
> would use staticmethod to make this clear, too.

But the example you gave works just fine as written! You are only using 
your utility function as a function so there's no need for it to be a 
staticmethod or indeed any other sort of method.

To be a convincing use-case you would have to show a situation where 
something had to be both a static method and a utility method rather than 
just one or the other and also where you couldn't just have both.

If you can persuade me that you need _handle_bool as both a static method 
and a utility function, you probably also need to explain why you can't 
just use both:

class Parser:
    def _handle_bool(input): ...
    handle_bool = staticmethod(_handle_bool)


-- 
Duncan Booth http://kupuguy.blogspot.com

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


Thread

Callable or not callable, that is the question! Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-07-11 15:05 +0200
  Re: Callable or not callable, that is the question! Peter Otten <__peter__@web.de> - 2013-07-11 16:11 +0200
    Re: Callable or not callable, that is the question! Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-07-12 08:41 +0200
      Re: Callable or not callable, that is the question! Duncan Booth <duncan.booth@invalid.invalid> - 2013-07-12 07:36 +0000
        Re: Callable or not callable, that is the question! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-12 09:49 +0000
          Re: Callable or not callable, that is the question! Peter Otten <__peter__@web.de> - 2013-07-12 12:36 +0200
  Re: Callable or not callable, that is the question! Jason Swails <jason.swails@gmail.com> - 2013-07-11 10:28 -0400
  Re: Callable or not callable, that is the question! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-12 02:12 +0000
    Re: Callable or not callable, that is the question! Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-12 01:11 -0600

csiph-web