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

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to