Python Forum
Is changing processes by using match with type function impossible?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is changing processes by using match with type function impossible?
#1
Hello.

I know to distinguish 'type' is usually with isinstance function, but examine this:

def what_is_it(x):
    def format():
        nonlocal x
        match type(x):
            case "<class 'float'>":
                return "a float"
            case "<class 'boolean'>":
                return "boolean"
            case "<class 'list'>":
                return "a list"
            case _:
                return f'a(n) {type(x)}\n'
    return f"{x} is {format()}."
However, this does not work properly.

>>> [what_is_it(i) for i in [False, ["a", "b"], 7.0, 7]]
["False is a(n) <class 'bool'>\n.", "['a', 'b'] is a(n) <class 'list'>\n.", "7.0 is a(n) <class 'float'>\n.", "7 is a(n) <class 'int'>\n."]
Is there any idea to let it work properly? Otherwise, is the combination of match and type no good?

Thanks.

Note: People familiar with ANSI Common Lisp would notice that what I want to do is something like typecase macro there.
Reply
#2
I found it seems to work, I don't know whether this is a good way to do or not, though.

def what_is_it(x):
    def format():
        nonlocal x
        match type(x).__name__:
            case 'float':
                return "a float"
            case 'bool':
                return "boolean"
            case 'list':
                return "a list"
            case _:
                return f'a(n) {type(x)}\n'
    return f"{x} is {format()}."
Anyway, it gives what I want.

[what_is_it(i) for i in [False, ["a", "b"], 7.0, 7]]
['False is boolean.', "['a', 'b'] is a list.", '7.0 is a float.', "7 is a(n) <class 'int'>\n."]
Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  determine parameter type in definition function akbarza 1 704 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  i want to use type= as a function/method keyword argument Skaperen 9 2,130 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  processes shall be parallel flash77 4 1,277 Sep-20-2022, 11:46 AM
Last Post: DeaD_EyE
  Impossible to cleanly uninstall Python 3.8.9 Ftaniere 2 1,873 Sep-01-2022, 10:38 AM
Last Post: snippsat
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,928 Jul-05-2022, 11:29 PM
Last Post: deanhystad
  Sharing imported modules with Sub Processes? Stubblemonster 2 1,617 May-02-2022, 06:42 AM
Last Post: Stubblemonster
  match type with value in csv parsing function vinci 2 1,428 Jan-21-2022, 12:19 PM
Last Post: Larz60+
  Killing processes via python Lavina 2 2,784 Aug-04-2021, 06:20 AM
Last Post: warnerarc
  sharing variables between two processes Kiyoshi767 1 1,977 Nov-07-2020, 04:00 AM
Last Post: ndc85430
  2 or more processes on the write end of the same pipe Skaperen 4 4,122 Sep-27-2020, 06:41 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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