Python Forum
redefinition of unused function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
redefinition of unused function
#16
(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!
Reply


Messages In This Thread
redefinition of unused function - by bb8 - Feb-09-2018, 07:00 PM
RE: redefinition of unused function - by buran - Feb-09-2018, 07:36 PM
RE: redefinition of unused function - by bb8 - Feb-10-2018, 05:42 AM
RE: redefinition of unused function - by DeaD_EyE - Feb-10-2018, 06:22 AM
RE: redefinition of unused function - by buran - Feb-10-2018, 06:41 AM
RE: redefinition of unused function - by wavic - Feb-10-2018, 08:29 AM
RE: redefinition of unused function - by bb8 - Feb-10-2018, 10:31 AM
RE: redefinition of unused function - by wavic - Feb-10-2018, 11:26 AM
RE: redefinition of unused function - by bb8 - Feb-10-2018, 02:03 PM
RE: redefinition of unused function - by wavic - Feb-10-2018, 03:14 PM
RE: redefinition of unused function - by bb8 - Feb-10-2018, 04:05 PM
RE: redefinition of unused function - by wavic - Feb-10-2018, 06:00 PM
RE: redefinition of unused function - by DeaD_EyE - Feb-10-2018, 07:02 PM
RE: redefinition of unused function - by bb8 - Feb-10-2018, 06:48 PM
RE: redefinition of unused function - by bb8 - Feb-22-2018, 12:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Uninstall unused COM ports windows 10 adbrooker 1 2,127 Sep-22-2021, 03:16 AM
Last Post: Larz60+
  redefinition of a method in a class: pylint warning kboo 1 4,365 Feb-13-2018, 11:21 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020