mastodon.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
The original server operated by the Mastodon gGmbH non-profit

Administered by:

Server stats:

352K
active users

@glyph You asked why not all classes should be dataclasses: My first thought was about defaults: The __eq__ default of the dataclass (which says that it's equal when the values are equal) is unlike how normal classes work and unexpected.

And this becomes a bigger problem when you use @property to set values.

@mborus in my ideal world this would raise an exception since this class doesn’t have a __c attribute

Glyph

@mborus how "unexpected"? this is what the distinction between `is` and `==` is for.

@glyph You have a point there, I should use "is" instead of "==" to compare for equal

Unexpected just because "==" ususally worked like "is" unless it's defined differently.

@mborus `==` is supposed to mean "value equality" whereas `is` is supposed to mean "mutability equality". built-in types follow this convention, so why are user-defined types confusingly different?

>>> x = [1, 2, 3]
>>> y = [1, 2, 3]
>>> a = {1: 2, 3: 4}
>>> b = {1: 2, 3: 4}
>>> print(x == y)
True
>>> x.append(1)
>>> print(x == y)
False
>>> print(a == b)
True
>>> b[5] = 6
>>> print(a == b)
False

@glyph Good question.

I don't know why this was chosen. If I had to guess using "is" as a default for "__eq__" is easy to implement and guaranteed that there's always a result to "==".

I always treat "==" as "whatever __eq__ says or defaults to" and changed it for some of my own classes.

To check I looked up the docs (docs.python.org/release/3.11.2) and "==" is just defined as "equal" (with no concrete definition what that actually means and the class default of "is" is mentioned)

Python documentation
Python documentationBuilt-in TypesThe following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, classes, instances and exceptions. Some colle...