[Python] How to use pattern matching with objects?

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following python code that converts an object into a view-like mapping.

    from collections.abc import Mapping

    class ObjView(Mapping):
    def __init__(self, obj):
    self.obj = obj

    def __contains__(self, key):
    return key in dir(self.obj)
    #return hasattr(self.obj, key) # Better, see the answers.

    def __getitem__(self, key):
    return getattr(self.obj, key)

    def __len__(self):
    return len(dir(self.obj))

    def __iter__(self):
    return iter(dir(self.obj))


    The use-case I have in mind is pattern-matching class elements:

    class Bird():
    ...
    def type(self):
    match ObjView(self):
    case {'quacks': True}:
    return 'Duck'
    case {'wings': [w1, w2, w3], 'flies': False, 'colors': ['black', 'white']}:
    return 'Three-winged penguin'
    ...


    The good thing is that an AttributeError is raised if the key is not an attribute, instead of the KeyError that gets caught in dict pattern-matching. I know the example is a bit silly, but in my situation a lot of the attributes are tuples of length 0 to 3, and I want to pattern-match them.

    Question. Can I achieve this type of pattern-matching behaviour using builtins instead of creating a custom ObjView class?

    Continue reading...

Compartilhe esta Página