Inheriting from str

ast ast at invalid
Mon Sep 20 10:08:41 EDT 2021


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)


More information about the Python-list mailing list