1
\$\begingroup\$

I'm wondering what is the best way to check for input arguments for a defining a class attributes, and performing type checking and value checking. Currently, I'm assigning the attributes and then checking the attributes, rather than checking the input arguments:

class A:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
        self._check_attr()

    def _check_attr(self):
        # check types.
        if not isinstance(self.a, TypeA):
            ...

        if not isinstance(self.b, TypeB):
            ...

Mainly because I want to use self._check_attr() to check for type and value using one function. Is this a cleaner way to do so? What is the standard way to ensure type and value safety in a python class?

With reference to Python 3.

\$\endgroup\$
  • 2
    \$\begingroup\$ Missing Review Context: Code Review requires concrete code from a project, with enough code and / or context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site. \$\endgroup\$ – Reinderien Jul 26 at 14:25
  • 1
    \$\begingroup\$ This is unfortunately off-topic due to being hypothetical. \$\endgroup\$ – Reinderien Jul 26 at 14:26
  • 1
    \$\begingroup\$ Let's please continue the conversation in chat.stackexchange.com/rooms/info/111055/… \$\endgroup\$ – Reinderien Jul 26 at 14:27