this post was submitted on 05 Jan 2024
9 points (76.5% liked)

Python

7250 readers
3 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
top 5 comments
sorted by: hot top controversial new old
[–] troyunrau@lemmy.ca 15 points 2 years ago

Tl;dr: don't use @property during design, it's almost certainly the wrong pattern. But use it to retrofit a method to a value.

[–] souperk@reddthat.com 11 points 2 years ago (1 children)

this is a method, and always was a method, I just wanted it to look like an attribute for aesthetic reasons

I think "aesthetic reasons" is an oversimplification. There are certain assumptions a developer makes when reading some code that uses properties. While these assumptions are not clearly defined and may differ per developer, I think there is a common core.

(1) There are no side-effects. The object is not mutated (or any other object), no IO takes place.

(2) The time and space complexity is O(1).

(3) The result is consistent. Consequent calls to the property should return the same value unless there is a mutation between them.

[–] VitulusAureus@lemmy.world 3 points 2 years ago

The combination of properties 1 and 3 makes it a pure function, which is also useful in compiled or jittable languages because it allows for a variety of optimizations.

[–] souperk@reddthat.com 5 points 2 years ago* (last edited 2 years ago)

(tried to delete this comment but somehow didn't work, I re-read the article and have a different outlook, sorry for the low effort comment)

[–] synae@lemmy.sdf.org 2 points 2 years ago

Nah, I think I'll continue to.