Please consider using composition over inheritance #50
Replies: 11 comments
-
First of all, this is not true for the C Extension. About the pure py implementation, originally it was designed this way. But accessing So I tried to use PS: usually I prefer composition |
Beta Was this translation helpful? Give feedback.
-
I don't think it's a reasonable requirement for the immutabilty to be impossible to circumvent. No other Python library goes to such an extent of preventing users from circumventing guarantees. In Python, this is known as the principle of consenting adults. However, it is a problem for
I figured that was the reason. I'm surprised that even with a C extension, it's still too slow to use composition. |
Beta Was this translation helpful? Give feedback.
-
I think it's more than resonable, is a must-have for an immutable type.
The other libraries does not implements immutable types.
Do you have practical use cases?
The C Extension does not need composition, since.... well, is written in C. All you said applies only to the pure py version. The C extension is not affected by this problem:
|
Beta Was this translation helpful? Give feedback.
-
I believe you're mistaken here. For example, a frozen dataclass, which is part of the standard library can have immutability circumvented using
Then what's the cost of fixing the Python version so that it's a Mapping but not a MutableMapping? If the C version is fast, what does it matter how the Python version is implemented?
Great! So, you should at least register |
Beta Was this translation helpful? Give feedback.
-
Indeed I use it in my py code, or I can't even set private attributes. But there's a huge difference in using
The C extension does not build on Windows, for example.
Already done, but thank you. |
Beta Was this translation helpful? Give feedback.
-
I don't think there's a difference. If your goal is to build some kind of security by obfuscation, then you can call the private variable
But this means that you different behavior on Windows then?
Awesome! |
Beta Was this translation helpful? Give feedback.
-
I'm quite sure that this is not "security by obscurity". Since this is Python, this is the best I found to create a reasonably immutable dict without sacrificing speed. Consider that also inheriting from Mapping and using a You can always use MappingProxyType. It's really slow, but it does not have this problem.
This means that the pure py version has this problem, and, as I already said, I don't see any real and practical reasons to change that. |
Beta Was this translation helpful? Give feedback.
-
It's better than nothing, but that's a very flimsy way of fixing the problem. If another library comes along and tries that approach, it will clobber your change. But I understand your desire for speed. |
Beta Was this translation helpful? Give feedback.
-
How? |
Beta Was this translation helpful? Give feedback.
-
Now that I have more time I understand what you intended. Listen to me. There are some points that maybe are not clear:
Good holidays. |
Beta Was this translation helpful? Give feedback.
-
In my opinion,
frozendict
shouldn't inherit fromdict
and thereforeMutableMapping
. Ideally, you would inherit fromMapping
, and use composition over inheritance. I realize that may be a significant change, but it would make the code more correct.This means doing something like:
Beta Was this translation helpful? Give feedback.
All reactions