D
D
Daaeev2020-12-18 22:30:26
Python
Daaeev, 2020-12-18 22:30:26

How to get rid of recursion in __settatr__ method?

How to correctly assign an attribute to an object in the __setattr__(instead of print('DONE')) method of a derived class so that there is no recursion?

class Calendar:
    __slots__ = ('D', 'M', 'Y')

    def __init__(self, d = 0, m = 0, y = 0):
        self.D = d
        self.M = m
        self.Y = y
        print(self)

    def __setattr__(self, key, value):
        if isinstance(value, int):
            print('ГОТОВО')
        else:
            raise AttributeError

    def __getattr__(self, item):
        print('ГОТОВО2')


c = Calendar(1, 12, 2004)

PS I'm a newbie, if I called something wrong - sorry)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-12-18
@Daaeev

ok google, "__setattr__ recursive"
PS I don't understand why a newbie should use __slots__

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question