Op 3/12/2024 om 10:41 schreef Roel Schroeven via Python-list:
[...]
When I try the same with bytes as base class though, that doesn't work (at least in the Python version I'm using, which is CPython 3.11.2 64-bit on Windows 10):

class MyBytes(bytes):
    def __init__(self, data):
        super().__init__(data)
print(MyBytes(b'abcdefghijlkmn'))

This results in an exception:

Traceback (most recent call last):
  File "test_mybytes.py", line 4, in <module>
    print(MyBytes(b'abcdefghijlkmn'))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "test_mybytes.py", line 3, in __init__
    super().__init__(data)
TypeError: object.__init__() takes exactly one argument (the instance to initialize)

I'm passing two arguments (data and the implicit self), and apparently that's one too many. Let's try without arguments (i.e. only the implicit self):

class MyBytes(bytes):
    def __init__(self, data):
        super().__init__()
print(MyBytes(b'abcdefghijlkmn'))

Now it works, and prints b'abcdefghijlkmn'. The same happens with int as base class, and presumably a number of other classes.

As a follow-up, it looks like this behavior is because bytes and int are immutable. When I try with bytesarray instead of bytes, which works largely the same but is mutable, things do work as I expect. There's a hint in the documentation of __new__(): "__new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation". But that doesn't tell me why using super().__init__(<custom arguments>) doesn't work for immutable classes.

The documentation for __init__() says " If a base class has an __init__() method, the derived class’s __init__() method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: super().__init__([args...])". So does that mean that bytes and int not have an __init__() method? Is there a link between being immutable and not having __init__()?

--
"Man had always assumed that he was more intelligent than dolphins because
he had achieved so much — the wheel, New York, wars and so on — whilst all
the dolphins had ever done was muck about in the water having a good time.
But conversely, the dolphins had always believed that they were far more
intelligent than man — for precisely the same reasons."
        -- Douglas Adams

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to