(Feb-10-2018, 06:00 PM)wavic Wrote: Hm! What does pythonic mean, then?
I do not know all built-in functions and modules. Perhaps there is a better way. As R. Hettinger says. I didn't read the previous posts very well. @Gribouillis mentioned it - from the functools module singledispatch.
def func(something): if isinstance(something, (int, float)): return something ** 2 elif isinstance(something, bytes): return something.decode(errors='IGNORE') elif isinstance(something, (list, tuple, dict)): return list(something) else: raise Exception('Input type not supported')I like the syntactic sugar of signledispatch.
Now the same function with singledispatch:
@singledispatch def func(something): raise Exception('Input type not supported') @func.register(float) @func.register(int) def _(something): return something ** 2 @func.register(bytes) def _(something): return something.decode(errors='IGNORE') @func.register(list) @func.register(tuple) @func.register(dict) @func.register(set) def _(something): return list(something)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!