I am required to make a FancyTuple class and optionally pass five values as I want, the FancyTuple class should return a specific value when accessed, for example when I pass:
- FancyTuple('dog','cat').first , it should return dog as dog is the first value passed.
- FancyTuple('dog','eagle','mouse).third should return mouse as mouse is the third element passed.
However when a non-initialized variable is accessed it should Raise an exception of variable not defined, example:
- FancyTuple('dog','cat').fifth , it returns an Exception because fifth has not been defined now.
How may I implement this? What I have been trying until now was to initialize in __ init__ and then in __str __ method I was trying to implement the null value but it is not working properly. I also tried __getattribute __ method but it gave a recursive code error?
The code is below:
class FancyTuple:
def __init__(self, first = None, second = None, third = None, fourth = None, fifth = None):
self.first = first
self.second = second
self.third = third
self.fourth = fourth
self.fifth = fifth
def __str__(self, value):
print('Value')
if value == None:
raise AttributeError('Accessed var is none')
return f'{self.first} {self.second} {self.third} {self.fourth} {self.fifth}'
Exceptionor custommessagewithout exception? @waasss