Inside a Python class, I have to do multiple checks for several private variables, e. g. self.__a
, self.__b
and self.__c
that are contained inside a list. The procedure would then start like this:
for var in [self.__a, self.__b, self.__c]:
...
My problem now is, that some (and possibly all) of the variables contained inside the list might have not been set up by now, and my program stops due to the occurring AttributeError
.
The only way I figured out to set up this list so that it contains only the variables that already exist is to write (in this case three) try-except-clauses of which every single one adds one of the variable to the list. But I think there has to be a better way to solve this task.
How can I do this in a more elegant way?