0

Formally, the lambda body can contain literals, identifiers, and operators. Why doesn’t Python understand the lambda body for such property implementation?

    class Test:
        test = property(
            lambda self: self.__test,
            lambda self, what: self.__test := what,
            None,
            'The property on lambdas')
  • You could use `property(lambda self: getattr(self, '__test'), lambda self, what: setattr(self, '__test', what)` – Unmitigated Feb 20 '23 at 18:52
  • 1
    `:=` is restricted in its use to restrict you from using it to produce spaghetti code, and lambdas are restricted for the same reason. Arguably in this case there's little reason to write this kind of code, as you may just as well make `test` a simple public attribute. The setters and getters don't do anything but assign and return the plain value without additional validation, so why this property wrapper in the first place? A properly useful setter or getter would require a full `def`, which is what Python wants you to write in the first place for readability. – deceze Feb 20 '23 at 18:56
  • What is "spaghetti code"? I don't listen about such idiom. Please, show example. – М. Ю. Брызгалов Feb 21 '23 at 09:18
  • https://en.wikipedia.org/wiki/Spaghetti_code — In this case it's about squeezing too much code into too little space. I.e. the restrictions are trying to prevent you from writing long single lines of code which does a lot in very little space and is thus hard to understand and maintain. Not sure there's a more targeted term for that than "spaghetti", but you'll get the idea. – deceze Feb 21 '23 at 09:26

0 Answers0