Skip to content

Entries published on December 21, 2023

1 entry published on this date. See also: all entries published in December 2023, full archive.

Don’t use Python’s property

This is part of a series of posts I’m doing as a sort of Python/Django Advent calendar, offering a small tip or piece of information each day from the first Sunday of Advent through Christmas Eve. See the first post for an introduction.

Attributing the problem

Suppose you’re writing Java and you write a class with an attribute :

public class MyClass {
    public int value;
}

And then later on you realize that value really wants to have some extra work happen at runtime — maybe it actually needs to be calculated “live” from other values, or needs auditing logic to note accesses, or similar. So what you actually want is something more like:

public class MyClass {
    private int value;

    public int getValue() {
        // Do the work and return the value...
    }
}

Except, at this point, you can’t turn the integer attribute someValue into an …

Entry published December 21, 2023. Read full entry.