3 ms·
Where did you get immutable data structures from in Python, and what do you do when every third party library you interact with uses mutable ones?
by tome 11y ago
Where did you get immutable data structures from in Python, and what do you do when every third party library you interact with uses mutable ones?
- icebraining 11y agoBesides tuples, I mostly treat the standard data structures as if they were immutable, without relying on them actually being :) As for third-party libraries, very few of them actually mutate structures passed by my code, in my experience; if they do, I simply make a copy before passing it.
- devishard 11y ago> Where did you get immutable data structures from in Python Here's my favored source (in the Standard Library) https://docs.python.org/2/library/collections.html#collections.namedtuple https://docs.python.org/2/library/collections.html#collectio... > and what do you do when every third party library you interact with uses mutable ones? That's situational; which library are you having a problem with? I haven't really had that problem (okay, sometimes the tooling around web frameworks do stupid things, but that's not common). Note that just because something is mutable doesn't mean you have to mutate it. Yes, Python doesn't as widely enforce non-mutation as Haskell, OCaml, etc., but that doesn't mean that suddenly all the Python programmers lost their minds and started mutating all the things. We're not savages.
- tome 11y ago> Note that just because something is mutable doesn't mean you have to mutate it I agree, and I don't mutate it, but my third party libraries and my collegues functions will. Not all of them, but enough to introduce annoying bugs.
- devishard 11y agoThis is why I asked, "Which library are you having a problem with?" You've claimed that third party libraries and your colleague's functions will mutate the data structures you pass them, but this has very rarely been my experience.