3 ms·
>A significant reduction in the size of a class instance in RAM is achieved by eliminating __dict__ and__weakref__. This is possible with the help of a "trick"
by wil421 7y ago
>A significant reduction in the size of a class instance in RAM is achieved by eliminating __dict__ and__weakref__. This is possible with the help of a "trick" with __slots__:
What is the downside to the __slots__: trick? In what cases do you need the dict and weakref?
- weberc2 7y agoIf you are writing code that adds or removes properties at runtime. I.e., when you want to write shoddy code.
- SEJeff 7y ago"when you want to write shoddy code" - This so much!
- hermitdev 7y agoI have written code like that in the past, and would consider doing so again in the future. When, it's appropriate, it can be hugely beneficial. When I last did it, I was wrapping a C++ API that I needed to compare against another dataset for a merge. The C++ API (which, admittedly, I also wrote), didn't have equality or hash operators defined on the Python objects. So, I monkey-patched them in for the keys I needed. It was actually the most elegant solution I could come up with as I could then naturally use the objects in sets and easily get differences to update the appropriate dataset. As an aside, when I wrote the python wrapper around my C++ API, I purposely didn't define equals and hash operators for the objects, despite having full information to do so on the natural keys, because I wanted the flexibility to do the monkey-patching and override how the objects were compared depending upon circumstance.
- war1025 7y agoOne big upside of using __slots__, which the docs tell you not to use it for, is that it will throw an error if you try to access or assign to a member that is not one of the slots. Very handy for catching bugs.
- piker 7y agoTry C++!
- war1025 7y agoI do what I'm paid to do :)
- wil421 7y agoLearning Python and Flask for my home projects. It’s really making me wish I was paid to write Python instead of JavaScript.
- mplanchard 7y agoIt’s not all roses! In my experience at three separate organizations, Flask apps tend to become horrific to maintain beyond a certain size, unless the original developers really know what they’re doing and are disciplined about it. A lot of it is due to the standard patterns for small Flask apps (e.g. instantiating your app directly at the module level rather than in a factory function, putting all of your business logic into ORM classes) causing increased complexity and unintended side effects down the line. Flask apps also struggle quite a bit with performance, and the only way to do things asynchronously is with something like celery. For Python, aiohttp is also a really interesting web framework, and the asynchronicity should be easy to grok for JS devs. Personally, as someone who has worked with Python/Flask professionally for years, I’d pick TypeScript and any JS web framework any day!
- merlincorey 7y ago> Flask apps also struggle quite a bit with performance, and the only way to do things asynchronously is with something like celery. I present to you, Quart[0], a flask-like web framework using Python 3.7's asyncio framework. It works pretty well in the single production application I have tested it in. [0] https://pgjones.gitlab.io/quart/ https://pgjones.gitlab.io/quart/
- war1025 7y agoA downside of __slots__ is that the way they are set up, you can only have slots come from one branch of an inheritance hierarchy, so they don't work out very well for fancy OO interface / mixin type stuff.
- gshulegaard 7y agoThere isn't really a "downside" in a strict sense; there are just trade-offs. The biggest one I run into is that __slots__ does not play well with multi-inheritance: https://stackoverflow.com/questions/472000/usage-of-slots https://stackoverflow.com/questions/472000/usage-of-slots Also, slots somewhat contributes to obfuscation since slotted objects don't work in the exact same ways as regular objects do so developers may run into weird stack traces they might not immediately recognize.
- stirfrykitty 7y ago>> developers may run into weird stack traces they might not immediately recognize Truth. To me anyway, debugging code is massively harder than writing it in the first place. Nothing worse than trying to fish out some odd issue that plagues you after spending two hours writing some code, and then 4 hours sorting it out.
- stirfrykitty 7y agoInheritance. Even Guido has said that people should avoid use of _slots_. Some people think that the space saving trumps the inheritance issue/type checking not inherited or some such. It's almost anti Python-like. People want to use it so they don't have to go down to something like C. I personally would not use _slots_ but then again, I'm just a nobody who writes semi-decent code for semi-decent money. YMMV.