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")
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
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
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
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
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):
>
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') __
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,