Callable or not callable, that is the question!

Duncan Booth duncan.booth at invalid.invalid
Fri Jul 12 09:36:30 CEST 2013

Ulrich Eckhardt <ulrich.eckhardt at> 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

More information about the Python-list mailing list