I try to treat real/physical Python objects as disposable. Everything in the runtime can be recreated from something else - be it a file on disk, a table in a DB, redis, a blob on S3, etc… and try not to put too much weight on the actual objects themselves. Modifying an object is really not something I ever do. I/O is dealt with elsewhere. I guess you could call it a hybrid approach of functional programming where ob…
Show HN: Gelidum – My Python library to freeze objects
21–30 of 31 posts
Re: Show HN: Gelidum – My Python library to freeze objects
#22# on_update="nothing": does nothing when an update is tried frozen_shared_state = freeze(shared_state, on_update="nothing") frozen_shared_state.count = 4 # Does nothing, as this update did not exist yikes. Thoughts on when this feature would ever be useful? Just the thought of working in a codebase with this subtle inconsistency makes me cringe.
I added this feature yesterday. Note the default value is "exception". With "nothin" I was thinking in passing some frozen object through a pipeline of unsafe/inherited/bad code, but without polluting the console with warnings or stopping the execution with exceptions.
Re: Show HN: Gelidum – My Python library to freeze objects
#23I like the purity that might come with decorating basically everything in a codebase with `@freeze_params()`, although I wonder/worry about what the runtime overhead might be. I really wish that something like that could be checked statically.
Performance is bad, I mean, all params are deep-copied because I assume frozen then "inplace" is not intended as they could be objects used by other parts of code.
Re: Show HN: Gelidum – My Python library to freeze objects
#24I like the purity that might come with decorating basically everything in a codebase with `@freeze_params()`, although I wonder/worry about what the runtime overhead might be. I really wish that something like that could be checked statically.
I'd really love if that could checked statically. Performance is bad, I mean, all params are deep-copied because I assume frozen then "inplace" is not intended as they could be objects used by other parts of code.
It looks like mypy can actually understand the hint: https://mypy.readthedocs.io/en/stable/final_attrs.html
It sucks that this would require such a substantial effort throughout a codebase, though. I guess that’s just the inherent cost of typed python.
Re: Show HN: Gelidum – My Python library to freeze objects
#25 @dataclass(frozen=True)
this tends to be enough for my usecases. you can still circumvent (and sometimes have to in dataclass post_init) with object.__setattr__.Re: Show HN: Gelidum – My Python library to freeze objects
#26FYI there is also @dataclass(frozen=True) this tends to be enough for my usecases. you can still circumvent (and sometimes have to in dataclass post_init) with object.__setattr__.
Having said that, I think dataclasses are a better solution for making immutable objects if the class is going to be made from the start.
Re: Show HN: Gelidum – My Python library to freeze objects
#27Earlier quoted context omitted.
I'd really love if that could checked statically. Performance is bad, I mean, all params are deep-copied because I assume frozen then "inplace" is not intended as they could be objects used by other parts of code.
I started googling around and I found the following: https://www.python.org/dev/peps/pep-0591/ It looks like mypy can actually understand the hint: https://mypy.readthedocs.io/en/stable/final_attrs.html It sucks that this would require such a substantial effort throughout a codebase, though. I guess that’s just the inherent cost of typed python.
Thank you for your input!
Re: Show HN: Gelidum – My Python library to freeze objects
#28Earlier quoted context omitted.
Speaking as someone who Friday had to fix an embarrassing bug in my Python code because lists aren’t immutable when passed to another function, I like the way you think. I will say, however, that what I want from Python isn’t actually “frozen” data structures but cloned ones (or, like some/most FP languages, structures with nested values that are only cloned when necessary). Vals, not vars.
Interesting idea, instead of a deep clone, would it be something similar to the copy-on-modify pattern?
https://hypirion.com/musings/understanding-persistent-vector...
Re: Show HN: Gelidum – My Python library to freeze objects
#29Earlier quoted context omitted.
Speaking as someone who Friday had to fix an embarrassing bug in my Python code because lists aren’t immutable when passed to another function, I like the way you think. I will say, however, that what I want from Python isn’t actually “frozen” data structures but cloned ones (or, like some/most FP languages, structures with nested values that are only cloned when necessary). Vals, not vars.
> lists aren’t immutable when passed to another function Well, lists are never immutable in Python; you want to use tuples for that.
I’ve only used tuples in Python as a small data structure, will have to experiment with using them as a list substitute, thanks.
Re: Show HN: Gelidum – My Python library to freeze objects
#30Earlier quoted context omitted.
> lists aren’t immutable when passed to another function Well, lists are never immutable in Python; you want to use tuples for that.
True, what I really want is referential transparency, not immutability per se. I’ve only used tuples in Python as a small data structure, will have to experiment with using them as a list substitute, thanks.