Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Inheriting from str

32 views
Skip to first unread message

ast

unread,
Sep 20, 2021, 10:08:53 AM9/20/21
to
Hello

class NewStr(str):
def __init__(self, s):
self.l = len(s)

Normaly str is an immutable type so it can't be modified
after creation with __new__

But the previous code is working well

obj = NewStr("qwerty")
obj.l
6

I don't understand why it's working ?


(python 3.9)

Jon Ribbens

unread,
Sep 20, 2021, 10:27:24 AM9/20/21
to
The string itself is immutable. If it's a subclass then it may have
attributes that are not immutable:

>>> s = 'hello'
>>> s.jam = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'jam'
>>> class foo(str): pass
...
>>> s = foo('hello')
>>> s.jam = 3
>>> s.jam
3

There's a lot of places in Python where you can break standard
assumptions with sufficiently evil or badly-written classes.
e.g.:

>>> class intt(int):
... def __add__(self, other):
... return int(self) + int(other) * 2
...
>>> a = intt(2)
>>> b = intt(3)
>>> a + b
8
>>> b + a
7

Julio Di Egidio

unread,
Sep 20, 2021, 10:46:54 AM9/20/21
to
On Monday, 20 September 2021 at 16:27:24 UTC+2, Jon Ribbens wrote:
> On 2021-09-20, ast <ast@invalid> wrote:
<snip>
> > I don't understand why it's working ?
>
> The string itself is immutable. If it's a subclass then it may have
> attributes that are not immutable:
>
> >>> s = 'hello'
> >>> s.jam = 3
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: 'str' object has no attribute 'jam'
> >>> class foo(str): pass
> ...
> >>> s = foo('hello')
> >>> s.jam = 3
> >>> s.jam
> 3

I think you have to (re)declare the __slots__, or you inherit members but in the usual __dict__:
<< The action of a __slots__ declaration is not limited to the class where it is defined. __slots__ declared in parents are available in child classes. However, child subclasses will get a __dict__ and __weakref__ unless they also define __slots__ (which should only contain names of any additional slots). >>
<https://docs.python.org/3.9/reference/datamodel.html#notes-on-using-slots>

Julio
0 new messages