Two lines of Python. Same result. Very different design. A method and a property can look almost interchangeable, but they communicate very different things about your code: cost, safety, and intent. Get that choice wrong, and you end up hiding work, I/O, or even async behavior behind what looks like a simple attribute access. In today’s video, I walk through clear, practical guidelines for deciding when something should be a property and when it should be a method. I’ll look at derived state, setters, Protocols, and why async properties are usually a design smell, even though Python technically allows them. 👉 Watch the full breakdown here: https://lnkd.in/eyXC6xyM. #python #softwaredesign #cleancode #objectoriented #apiDesign #developers
My rule for properties is: - If it IS a thing (objects, values, etc.) Then I make it a property, even if populating that value requires a slow API call, or a lot of calculations. - If it DOES some thing (saves data, updates something, has side effects...) Then I make it a method or function.
Arjan Egges I would appreciate it if you could do a deep dive video about asyncio, event loops and how asyncio works with other async libs (for example if third party lib uses its own event loop and you create yours with asyncio) and so on... For me the async world is full of mysteries, but very interesting 🙂
"Method disguised as property", blurs the line between interface (ABC) and implementation of a class, and the distinction between interface and implementation (especially how state is represented) is a key corner stone of OOP. Disguises don't express the intend of code clearly. There is no real difference between a property doing little work and later doing a lot of work. But hey, use it if when it brings you value, there are no hard rules anyway. Everybody is making up their own new rules/concepts/syntax all the time, what a beautiful mess.
I never even dream of using async property
Haha! Once, I was browsing through a codebase, and when I click on a property to see its declaration I saw hell! A property of maybe 100 lines of code, with complex logic, an the best! API calls in it! 😱 Wondering why the app had performance issues...
Arjan spreading wisdom. Love it
the kind of detail that separates code that works from code that lasts
You don't use async properties due to wisdom. I don't use async properties due to ignorance. We are not the same.
Nice video thanks !
I am genuinely curious: if the result is the same, why does the design matter?