whoareu to ProgrammingEnglish • 27 days agowhich python trick or hack you know that you would want to share?message-square45fedilinkarrow-up197arrow-down12
arrow-up195arrow-down1message-squarewhich python trick or hack you know that you would want to share?whoareu to ProgrammingEnglish • 27 days agomessage-square45fedilink
minus-square@[email protected]linkfedilinkEnglish6•edit-227 days agoYou can feign immutablility on class attributes by playing with __setattr__. I don’t remember the exact way to do it, but its roughly like this: class YourClass: def __setattr__(self, name, value): if not hasattr(self, name): super().__setattr__(name, value) else: # handle as you wish if the # attr/value already exists. # pass, raise, whatever I say “feign immutability” because there are still cases in which an attr value can change, such as: the underlying attribute contains a list and appending to the list the underlying attribute is a class and modifying the class’s attributes pretty much anything that does “in place” changes, because so much of python is referential and has side effects.
minus-square@[email protected]linkfedilink6•26 days agoI have to mention dataclasses here, especially with frozen=True. Seriously, use dataclasses whenever possible, they’re great.
You can feign immutablility on class attributes by playing with
__setattr__
. I don’t remember the exact way to do it, but its roughly like this:class YourClass: def __setattr__(self, name, value): if not hasattr(self, name): super().__setattr__(name, value) else: # handle as you wish if the # attr/value already exists. # pass, raise, whatever
I say “feign immutability” because there are still cases in which an attr value can change, such as:
I have to mention
dataclasses
here, especially withfrozen=True
.Seriously, use dataclasses whenever possible, they’re great.