I'm making a simple command-line game but not sure how to model read-only user inputs in Python.
Right now, I'm thinking of declaring an ABC called Input
and subclassing it:
import abc
class Input(abc.ABC):
@abc.abstractmethod
def is_valid(self, *args, **kwargs):
pass
@abc.abstractmethod
def pop(self):
pass
class NameInput(Input):
def __init__(self, name):
self._name = name
def is_valid(self, *args, **kwargs):
pass
def pop(self):
return self._name
Another way I came up with is using the @property
decorator:
import abc
class Input(abc.ABC):
@abc.abstractmethod
def is_valid(self, *args, **kwargs):
pass
@abc.abstractmethod
def value(self, value, *args, **kwargs):
pass
class NameInput(Input):
def __init__(self, name):
self._name = name
def is_valid(self, *args, **kwargs):
pass
@property
def value(self, value, *args, **kwargs):
return self._name
Is there a better way of accomplishing my objective? If so, what is it?
For your information, my goal is not just building a program up and running but picking up good programming habits. Please feel free to overcomplicate and tell me the way you prefer (for the sake of scalability and maintainability) in a large-scale project.