new match statement and types
Robin Becker
robin at reportlab.com
Mon Mar 15 08:11:06 EDT 2021
I'm trying out new features in 3.10.0a6 looking at the new match statement I tried it with various cases. Knowing that
the class of a class is 'type' I tried using match to distinguish between classes and instances so I tried various
versions of
#######################
class A:
pass
class B:
pass
def tmc(C):
match C:
case type(__name__='A'):
print(f'{C} is an A' )
case type():
print(f'{C} is a type' )
case A():
print(f'{C} is an A instance')
case _:
print(f'{C} is an instance')
if __name__=='__main__':
tmc(A)
tmc(B)
tmc(A())
tmc(B())
#######################
the above seems to work and produces
> <class '__main__.A'> is an A
> <class '__main__.B'> is a type
> <__main__.A object at 0x7fe5a2248fd0> is an A instance
> <__main__.B object at 0x7fe5a2248fd0> is an instance
is this the right approach to this problem of distinguishing instances ?
--
Robin Becker
More information about the Python-list
mailing list