class ColourComponent: def __init__(self, start, end): self.start = start self.end = end def __get__(self, instance, owner_class): # print(f'__get__ called') return int(instance.hex[self.start:self.end], 16) def __set__(self, instance, value): # print('__set__ called') pass class Colour: r = ColourComponent(1, 3) g = ColourComponent(3, 5) b = ColourComponent(5, 7) def __init__(self, hex): self.hex = hex def test_colour(): colour = Colour('#ff8823') print(colour.r) print(colour.g) print(colour.b) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class MaxLengthAttribute: def __get__(self, instance, owner_class): if instance.__dict__ == {}: return None return sorted(instance.__dict__.keys(), key= lambda x: (len(x), x), reverse=True)[0] class MyClass: max_length_attribute = MaxLengthAttribute() class JustClass: max_atr = MaxLengthAttribute() def test_len(): # obj = MyClass() # obj.name = "Vasiliy" # obj.city = "Saint Peterburg" # obj.country = "Rus" # print(obj.max_length_attribute) obj = JustClass() obj.mock = 15 obj.city = "Saint Peterburg" obj.name = "Vasiliy" obj.door = 'wood' print(obj.max_atr) if __name__ == '__main__': # test_colour() test_len()