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

How to correctly specify the type of the argument?

I have a ButtonTypes class:

class ButtonTypes:
    def __init__(self):
        self.textType = "text"
        self.callbackType = "callback"
        self.locationType = "location"
        self.someAnotherType = "someAnotherType"

And a function that should take one of the attributes of the ButtonTypes class as an argument:

def create_button(button_type):
    pass

How can I specify that the argument of the create_button function should not just be a string, but exactly one of the attributes of the ButtonTypes class?

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

Something like this:
def create_button(button_type: ButtonTypes.Type)

As far as I understand, I need to create a Type class inside the ButtonTypes class, and then many other classes for each type that inherit the Type class, but I think something in my train of thought is wrong.

>Solution :

It sounds like you actually want an Enum:

from enum import Enum

class ButtonTypes(Enum):
    textType = "text"
    callbackType = "callback"
    locationType = "location"
    someAnotherType = "someAnotherType"

def func(button_type: ButtonTypes):
    # Use button_type

The enum specifies a closed set of options that the variable must be a part of.

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