Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python property not working in match-case

I am trying to access Foo using Bar having a property.

class Foo:
    ONE = "one"
    TWO = "two"
    THREE = "three"

class Bar:

    @property
    def foo(self):
        return Foo



word = "one"

match word:
    case Foo.ONE:         # This is working
        print("GOOD")     

    case Bar().foo.ONE:   # This is not working
        print("BEST")

What I am missing?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You cannot use evaluations at this place. But you can do what you want with guards:

word = "one"

match word:
    
    case str(value) if value == Bar().foo.ONE:   
        print("BEST")
        
    case str(value) if value == Foo.ONE:         
        print("GOOD")     

The str() is not mandatory, it adds some checking on the type of word.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading