super().__init__() and bytes

Roel Schroeven roel at roelschroeven.net
Tue Dec 3 04:41:06 EST 2024


We can use super().__init__() in the __init__() method of a derived 
class to initialize its base class. For example:

import string
class MyTemplate(string.Template):
     def __init__(self, template_string):
         super().__init__(template_string)
print(MyTemplate('Hello ${name}').substitute(name="Pedro"))

This works, and prints "Hello Pedro" as expected. Note that I passed 
template_string in the super().__init__() call, and that is what used to 
initialize the base class. So far nothing special.

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. That behavior is 
beyond my understanding, so I have some questions that might hopefully 
lead to a better understanding:

(1) How does that work? How does my argument end up in the code that 
initializes the instance state?

(2) How can I customize the argument is passed? For example, what if I 
want to do something like (supersimple example) super().__init__(data * 2)?

(3) Why is bytes (and int, ...) different? Is it because it's a builtin? 
Or implemented in C? Or something else?

-- 
"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



More information about the Python-list mailing list