Deutsch   English   Français   Italiano  
<mailman.4.1733220066.2965.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: Roel Schroeven <roel@roelschroeven.net>
Newsgroups: comp.lang.python
Subject: Re: super().__init__() and bytes
Date: Tue, 3 Dec 2024 11:01:00 +0100
Lines: 57
Message-ID: <mailman.4.1733220066.2965.python-list@python.org>
References: <3cc6272f-b151-474a-a83c-7f3339734bf5@roelschroeven.net>
 <ce4e0a1b-229a-48ad-b256-5835e0e509cc@roelschroeven.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de WKjzUCWVfa9mjrrGmsR9zQpJNvJ7arMisz5qVS/AaTrQ==
Cancel-Lock: sha1:ZdTcQehobOUZDEwLDG06uLsOeiY= sha256:Ilf2agVlLZC08wEJI3sltUoybo9qUh8MKmb7OvfP2Xw=
Return-Path: <roel@roelschroeven.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
 reason="2048-bit key; unprotected key"
 header.d=roelschroeven.net header.i=@roelschroeven.net
 header.b=YIDBRLSH; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'looks': 0.02; 'argument':
 0.04; 'def': 0.04; 'traceback': 0.04; '(most': 0.05; 'class,':
 0.05; 'last):': 0.05; 'achieved': 0.07; 'explicitly': 0.07;
 'apparently': 0.09; 'derived': 0.09; 'example:': 0.09; 'implicit':
 0.09; 'int': 0.09; 'intelligent': 0.09; 'prints': 0.09; 'roel':
 0.09; 'schroeven': 0.09; 'typeerror:': 0.09; 'arguments': 0.16;
 'cpython': 0.16; 'hint': 0.16; 'instance': 0.16; 'method?': 0.16;
 'presumably': 0.16; 'reasons."': 0.16; 'received:10.202': 0.16;
 'received:10.202.2': 0.16; 'received:internal': 0.16;
 'received:messagingengine.com': 0.16; 'schreef': 0.16; 'subject:()
 ': 0.16; 'subject:super': 0.16; 'whilst': 0.16; 'python': 0.16;
 'says': 0.17; 'instead': 0.17; 'to:addr:python-list': 0.20;
 'skip:_ 10': 0.22; '\xe2\x80\x94': 0.22; 'version': 0.23; 'skip:p
 30': 0.23; 'behavior': 0.26; 'done': 0.28; 'header:User-Agent:1':
 0.30; 'takes': 0.31; "doesn't": 0.32; 'but': 0.32; "i'm": 0.33;
 'there': 0.33; 'header:In-Reply-To:1': 0.34; 'windows': 0.34;
 'same': 0.34; 'mean': 0.34; 'using': 0.37; 'class': 0.37; 'file':
 0.38; 'two': 0.39; "that's": 0.39; 'least': 0.39; 'base': 0.40;
 'try': 0.40; 'tell': 0.60; 'skip:o 10': 0.61; "there's": 0.61;
 'skip:\xc2 10': 0.62; 'ever': 0.63; 'between': 0.63; 'skip:b 10':
 0.63; 'mainly': 0.64; 'time.': 0.66; 'types': 0.67; 'exactly':
 0.68; 'customize': 0.69; 'received:202': 0.69; 'man': 0.74;
 '8bit%:100': 0.76; 'water': 0.81; 'happens': 0.84; '(like': 0.84;
 'dolphins': 0.84; 'method,': 0.84; 'adams': 0.91; 'largely': 0.91;
 'believed': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
 roelschroeven.net; h=cc:content-transfer-encoding:content-type
 :content-type:date:date:from:from:in-reply-to:in-reply-to
 :message-id:mime-version:references:reply-to:subject:subject:to
 :to; s=fm3; t=1733220062; x=1733306462; bh=n0Uem/0eugmROR78IO2tP
 61p6mUoRNnkF1+B8HhjGd4=; b=YIDBRLSHXsy7kzOezdbjYGpmyDisIZJF+U9sN
 OBjHoFSkGmkINRf9WJYja5RFRljNc7q2CTLPvchUTHb4+uqH6axhzgWg5CpA+5UJ
 Ka9ZEVpddNYsbHsFUSzifeHFH0rHWIbxgJkT4AZLcrIGuLa/vwxO75oaz3gss00G
 FiIXTfHh6gSo1hMP5APu7105L8csuEMz+1pd080UpK0bkAZGRnW0fLRn4nIdJGXb
 d/3lCtQO5+9N4IcUxy/85SHKKkTp7Gv8Qa/FB0r8FgjtteGgVirOxq2hXpwGzlBf
 19lEYojzssOfd1HtukrUMZKuZRViqMp0I9OTfaDIEGyAxP57g==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
 messagingengine.com; h=cc:content-transfer-encoding:content-type
 :content-type:date:date:feedback-id:feedback-id:from:from
 :in-reply-to:in-reply-to:message-id:mime-version:references
 :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender
 :x-me-sender:x-sasl-enc; s=fm1; t=1733220062; x=1733306462; bh=n
 0Uem/0eugmROR78IO2tP61p6mUoRNnkF1+B8HhjGd4=; b=vnsnSLOVv4YkO+yuC
 lzeECY6v/tsFQVHvIEKGPHQ3LUUcbB+i+FSPchh4xHDgfTwxEUrQvYZ9HA8I/xSG
 OwQZ/WI9QBuxr4/LUjqcDGdwD9oniS4jdSsHifI0UXn80LbwlYrHWjBXPVCiw+H5
 CKSxF8qEZzfggqFUj+R2ZJ9mdOJu1qOqrbcFQhayQ/kLhxhXu5U+thPfengRraRJ
 mIh/xe74XCPSq5LTC4VreoARdKgtrfRx7bJyLZIJSmUQb3uD/rMXDYZZF+WKCWjj
 tSIqV7DsTrAK3t7xDV7S/bG6TPOYixZZcQ3P+lKIsBG4RQv2B9ZXmCvlUAM6EyUg
 VFBHQ==
X-ME-Sender: <xms:3dZOZwIMRHBaI_ziLUtANPf-TMDPVIWHxmGGLJIIa5W4kh2fmK2HOw>
 <xme:3dZOZwIFkQURmdDXsYksdajYN6zipaMEBXHEAbWx2FUcCgR25fe49YEHYKd8_YXI7
 iVjjAWYiWnX>
X-ME-Received: <xmr:3dZOZwtGd_qj5ww4YVAadgeCoCze1Qyb76xtXMdr7OdjYqo_kGhh5-8PpQbZ8ehVppfdUZA0a7MSGIARwLUgCxRsggb3HkwMwY9Nd05zOaS23Jg>
X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddriedvgddutdcutefuodetggdotefrodftvf
 curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr
 tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepkfffgggfuf
 fvfhfhjggtgfesthekredttddvjeenucfhrhhomheptfhovghlucfutghhrhhovghvvghn
 uceorhhovghlsehrohgvlhhstghhrhhovghvvghnrdhnvghtqeenucggtffrrghtthgvrh
 hnpeegjeffkeegvdevjeejhfegtddvhefhiefhleefhffgiedtvdeigeeiieegvdduhfen
 ucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehrohgvlh
 esrhhovghlshgthhhrohgvvhgvnhdrnhgvthdpnhgspghrtghpthhtohepuddpmhhouggv
 pehsmhhtphhouhhtpdhrtghpthhtohepphihthhhohhnqdhlihhsthesphihthhhohhnrd
 horhhg
X-ME-Proxy: <xmx:3dZOZ9Ygvi5agmbwV7UrKOA65c6mdN6yx7RTJqrWYS2RIfxOvOQ_qw>
 <xmx:3dZOZ3YDAM6AwPvQwDwozFMmdAgYoyfkWuqqNiLfr1PKVj-zjox4Tw>
 <xmx:3dZOZ5AZzL5edcUBc5dRUtpGtuZUDUmFrfrWQIEF4RZV9QDDnGHjkg>
 <xmx:3dZOZ9bTO3-8NSvWFzPjJuADoOR0WgeqOPsEGLQjKx_v9X38Xj0zxQ>
 <xmx:3tZOZ-wwLOGMpiTJg-xtydNfUtUbGCcuKXgK-A8I6lyGZtUURGdcZ4Y8>
Feedback-ID: i8e5b41ae:Fastmail
User-Agent: Mozilla Thunderbird
Content-Language: nl, en-US
In-Reply-To: <3cc6272f-b151-474a-a83c-7f3339734bf5@roelschroeven.net>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
 <python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
 <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
 <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <ce4e0a1b-229a-48ad-b256-5835e0e509cc@roelschroeven.net>
X-Mailman-Original-References: <3cc6272f-b151-474a-a83c-7f3339734bf5@roelschroeven.net>
Bytes: 9036

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