Please consider using composition over inheritance #50
Replies: 12 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.
-
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.
-
It's a late reply, but I post this because I think I must. First of all, I misunderstood the post. I thought "flimsy" was an insult. My English was very bad and it's not improved. I use Context Reverso and AIs now. I'm sorry. Really. A normal reply should had been: Yes, it's true. The monkeypatch and the fact that, in pure py,
Anyway, you have also to consider this:
About immutability: it's undeniable that the pure py version is more immutable this way, and the primary goal of this library is... well, having an immutable dict. You firstly said no one pushed this so far to make an object immutable. Lately, you quoted About About Let me also say that your implementation of a Anyway, as I said, the pure py version is a little toy. It's only 254 lines, and I suspect that the 99% of people uses only less than one half of them, and in a wrong way :D Anyway, as you can see, I left the discussion open and I'll continue to leave it this way, because I'm always open to change my mind. I'm against censorship or absolutisms -- even if my last reply can be seen as the opposite! :-P I'm still continuing to develop the C extension, even if my time is very short now, but I don't think it will ever be implemented in CPython. At least not before I died X-D I don't really understand why, but anyway, hey, that's life. Furthermore, as a separate project, I'm free to add functional programming methods, like Anyway, I prefer a billion times having Furthermore, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In my opinion,
frozendictshouldn't inherit fromdictand 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