Re: Question about implementing immutability

2018-11-22 Thread Iwo Herka
czw., 22 lis 2018 o 11:14 Thomas Jollans napisał(a): > [..] this allows other classes' __init__s to set attributes. Fair point. > I might try setting a self._fixed flag at the end of init and do a check > > if getattr(self, '_fixed', False): > raise TypeError(f"'{type(self)}' is immutable")

Re: Question about implementing immutability

2018-11-22 Thread Thomas Jollans
On 2018-11-21 17:45, Iwo Herka wrote: > Hello, > > Let's say I want to implement immutability for user-defined class. > More precisely, a class that can be modified only in its (or its > super-class') __init__ method. My initial idea was to do it the > following fashion: > > def __setattr__(s

Re: Question about implementing immutability

2018-11-22 Thread Iwo Herka
czw., 22 lis 2018 o 10:53 Thomas Jollans napisał(a): > If you're tempted to go down that route and can require Python 3.7, use > dataclasses! I'm aware of them, thanks. :) Dataclasses are great for certain use-cases; I was just wondering how hard would it be to implement something approximating g

Re: Question about implementing immutability

2018-11-22 Thread Thomas Jollans
On 2018-11-21 21:36, Calvin Spealman wrote: > If you want to create your own immutable class, maybe inherit from a > namedtuple? If you're tempted to go down that route and can require Python 3.7, use dataclasses! > > On Wed, Nov 21, 2018 at 11:45 AM Iwo Herka wrote: > >> Hello, >> >> Let's s

Re: Question about implementing immutability

2018-11-22 Thread Iwo Herka
Dan Sommers <2qdxy4rzwzuui...@potatochowder.com> wrote: > If an instance of your class contains a list, and you change one > of the elements of that list, then the instance's __setattr__ > never comes into play: I think that's within the bounds of what is understood as "immutable" in Python. Tuple

Re: Question about implementing immutability

2018-11-21 Thread dieter
Iwo Herka writes: > Let's say I want to implement immutability for user-defined class. > More precisely, a class that can be modified only in its (or its > super-class') __init__ method. My initial idea was to do it the > following fashion: > > def __setattr__(self, *args, **kwargs): >

Re: Question about implementing immutability

2018-11-21 Thread Calvin Spealman
If you want to create your own immutable class, maybe inherit from a namedtuple? On Wed, Nov 21, 2018 at 11:45 AM Iwo Herka wrote: > Hello, > > Let's say I want to implement immutability for user-defined class. > More precisely, a class that can be modified only in its (or its > super-class') __

Re: Question about implementing immutability

2018-11-21 Thread Dan Sommers
On 11/21/18 11:45 AM, Iwo Herka wrote: Hello, Let's say I want to implement immutability for user-defined class. More precisely, a class that can be modified only in its (or its super-class') __init__ method. My initial idea was to do it the following fashion: def __setattr__(self, *args,