May-30-2024, 01:42 PM
Hello.
I know to distinguish 'type' is usually with isinstance function, but examine this:
Thanks.
Note: People familiar with ANSI Common Lisp would notice that what I want to do is something like typecase macro there.
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.