I have several classes that I don’t own. These classes provide methods to access internal information, rather than attributes or properties.
For example a class might look something like this:
class Foo:
def __init__(self, a, b) -> None:
self._a = a
self._b = b
def a(self): return self._a
def b(self): return self._b
def c(self): return self._a + self._b
I would like to be able to use match/case to do something like this:
foo_list = [
Foo(1,0),
Foo(0,1),
Foo(1,2),
Foo(3,3),
Foo(3,2),
]
for foo in foo_list:
match foo:
case Foo(a=a, b=0):
print(f'first a = {a}, b = {foo.b}')
case Foo(a=0, b=b):
print(f'second a = {foo.a}, b = {b}')
case Foo(a=1, b=b):
print(f'third a = {foo.a}, b = {b}')
case Foo(a=a, b=3):
print(f'forth a = {a}, b = {foo.b}')
case Foo(a=a, b=b, c=5):
print(f'fifth a = {a}, b = {b}, c = {foo.c}')`python
However this does not work since the case
statements matching an object expect constructor-like syntax where the named arguments are attributes or properties not getter methods, which is what I have.
What is the Pythonic way to match/case statements with classes such as this?