Issue
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?
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.
Answered By - Carcigenicate
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.