Deutsch   English   Français   Italiano  
<mailman.6.1733235899.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 15:24:55 +0100
Lines: 39
Message-ID: <mailman.6.1733235899.2965.python-list@python.org>
References: <3cc6272f-b151-474a-a83c-7f3339734bf5@roelschroeven.net>
 <ce4e0a1b-229a-48ad-b256-5835e0e509cc@roelschroeven.net>
 <VI1PR05MB106809E93AA17A3D46E380DFEB4362@VI1PR05MB10680.eurprd05.prod.outlook.com>
 <c5901834-52b0-4238-bda1-6b74220be68d@roelschroeven.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de 5667pQap5Rz/AkYQpmN+gwNgB1ckjS0IW5NfHR7vTFtA==
Cancel-Lock: sha1:/RcXiV5jbfKaF8eU2h6mtQ70hJU= sha256:pFUtBMLF80lJow+ZhNv2NbnEXS0f5EHlvx2bKgB6yiE=
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=JaCL38NF; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'looks': 0.02; 'def': 0.04;
 'attract': 0.09; 'int': 0.09; 'perfectly.': 0.09; 'roel': 0.09;
 'schroeven': 0.09; 'yes.': 0.09; 'that.': 0.15; '"there': 0.16;
 'attributes': 0.16; 'low-level': 0.16; 'nothing,': 0.16; 'ok.':
 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;
 'terribly': 0.16; 'wrote:': 0.16; "can't": 0.17; 'to:addr:python-
 list': 0.20; 'all,': 0.20; 'language': 0.21; "what's": 0.22;
 'cannot': 0.25; 'behavior': 0.26; 'classes': 0.26; 'do,': 0.26;
 'object': 0.26; "isn't": 0.27; 'think': 0.29; 'header:User-
 Agent:1': 0.30; 'takes': 0.31; 'before.': 0.31; "doesn't": 0.32;
 'but': 0.32; "i'm": 0.33; 'header:In-Reply-To:1': 0.34; 'same':
 0.34; "didn't": 0.34; 'trying': 0.35; 'cases': 0.36; 'work,':
 0.36; 'thanks,': 0.36; 'change': 0.36; 'necessarily': 0.37;
 'special': 0.37; 'using': 0.37; "it's": 0.37; 'class': 0.37;
 'though': 0.37; 'way': 0.38; 'use': 0.39; "that's": 0.39; 'on.':
 0.39; 'define': 0.40; 'something': 0.40; 'want': 0.40; 'tell':
 0.60; 'best': 0.61; 'method': 0.61; 'skip:o 10': 0.61; 'feel':
 0.63; 'skip:b 20': 0.63; 'skip:b 10': 0.63; 'clear': 0.64;
 'inherited': 0.84; 'method,': 0.84; 'received:103': 0.91
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=1733235896; x=1733322296; bh=mQLj9K/a3bhM32gthhmQM
 hjptUbc0WEcBF+F3m9v8FQ=; b=JaCL38NFLVSrIQAjFHLmsz+GFVuoRIAAjMZA2
 n9i6w/OBDqzqiz20efgX4qVhb4O/oXnkAAKVjvgchGA09/vbbcxKaUYV4KhQdoSM
 ZrCSuYObe8iKDPLuBkXs27Bn6eciWwaLv3M8FOd1RhC3WPS2Epwh2gMMoAMGlZY3
 VCnfYkvEF1aemejIbzpr2ALxJ/gJPKh2yAojtKUnj8TTFLuQWW9z4zkRx7VLNPao
 /Cm3ShVkkUnpZ5Oew3z3HTKp8Mw6hjo2fDtkDJsBlVJydwiZ/JGFDtv/xu0R6IXI
 RyW/KN0xFBPvPRRJw54QQ1gTGPidgzhLvuJoECX6728BXRkDg==
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=1733235896; x=1733322296; bh=m
 QLj9K/a3bhM32gthhmQMhjptUbc0WEcBF+F3m9v8FQ=; b=bum7s8hZ+fm7ZIcEX
 /m8QOQJZVW4xpj1W110fXSPoMw4KwrOl7PxHse+io0OGG5btwrWBpLSZ81fqi3PI
 Gel8kFZL8zxpZucNP3GH58a1Tw1Uqq6ziINe9YBFXrlGLxcKIsKdKHoe2vyr8hs7
 i/a6Z56YQexTTb8ei5t5N+HHUKz39BhhAztqOyU/EMR+BK86Unb0V1qfa4q7FNde
 q7TxGdmcb7WX8+lbT2F3ZEgsiLhLZ1kkHylaIKIm/OWpA+hjHk4VpHEiHLps1tlN
 RwX472roZntpBnaSaklRxwkAsjPphVrUKPOpvsmoJ57AumQBcZEu5ytk/6UgiCyu
 smgzQ==
X-ME-Sender: <xms:uBRPZ5fp3PaDeyzSkXdBgXQRHZgkOX6v6y-6DdwYcMIxLUq0yX1lUg>
 <xme:uBRPZ3M7Fyf0rjjxbxHa30c-eONAVGy4IIlhFVnfPMrN6qx0qiI1BBEaluiKMQIfS
 MwY1a8uzh-O>
X-ME-Received: <xmr:uBRPZyj6sQOoICfP776p-wFrO7R7rwblhUFe_0ngBX7ljoRC_lh_yuoveVRCs79Ji1FMSW_v6ra9q5yW4FRAwwsnMxer3ce1T7EqiVrFeJHIPH0>
X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrieefgdefhecutefuodetggdotefrodftvf
 curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr
 tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepkfffgggfuf
 fvfhfhjggtgfesthejredttddvjeenucfhrhhomheptfhovghlucfutghhrhhovghvvghn
 uceorhhovghlsehrohgvlhhstghhrhhovghvvghnrdhnvghtqeenucggtffrrghtthgvrh
 hnpeekgfefudeuhedvkeefjeehvdeljeelfffhtdegteejtdeufeeffefhveetudffueen
 ucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehrohgvlh
 esrhhovghlshgthhhrohgvvhgvnhdrnhgvthdpnhgspghrtghpthhtohepuddpmhhouggv
 pehsmhhtphhouhhtpdhrtghpthhtohepphihthhhohhnqdhlihhsthesphihthhhohhnrd
 horhhg
X-ME-Proxy: <xmx:uBRPZy_fSguXgvMKT8oJgMdTwAXwmxeDkVQOVhp7JDOBCxywi0FSpA>
 <xmx:uBRPZ1sF_dARPj_vRCg4cLZq9tpgMTN0ilA_ZvE4Q9ybdKeM3z2IHA>
 <xmx:uBRPZxGmVal-7fKYgC30SKD0mIZg1h_OY2D28oEpOUsGTcmzKy6NmQ>
 <xmx:uBRPZ8OM9FRdiBy840RTJ_Keq-LpGO2jLy__47DkCB8SmDFU3Kqrcw>
 <xmx:uBRPZ2UBZ4O0c8kiesTd6aXacYPH8e83LzyeyP7ZzKo4-dr6C4L1Wv9H>
Feedback-ID: i8e5b41ae:Fastmail
User-Agent: Mozilla Thunderbird
Content-Language: nl, en-US
In-Reply-To: <VI1PR05MB106809E93AA17A3D46E380DFEB4362@VI1PR05MB10680.eurprd05.prod.outlook.com>
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: <c5901834-52b0-4238-bda1-6b74220be68d@roelschroeven.net>
X-Mailman-Original-References: <3cc6272f-b151-474a-a83c-7f3339734bf5@roelschroeven.net>
 <ce4e0a1b-229a-48ad-b256-5835e0e509cc@roelschroeven.net>
 <VI1PR05MB106809E93AA17A3D46E380DFEB4362@VI1PR05MB10680.eurprd05.prod.outlook.com>
Bytes: 8436

Op 3/12/2024 om 13:55 schreef Anders Munch via Python-list:
> Roel Schroeven <roel@roelschroeven.net> wrote:
> > As a follow-up, it looks like this behavior is because bytes and int are immutable.
>
> Yes.
OK.
> > But that doesn't tell me why using super().__init__(<custom arguments>) doesn't work for immutable classes.
>
> bytes.__init__ does work, but it's just an inherited object.__init__, which does nothing, and takes no parameters.
>   __init__ cannot change the value of the bytes object; the value is set by bytes.__new__ and cannot change after that.

I see now why __init__, being a regular method, can't change an object's 
value (or attributes in general) if that object is immutable. I'm not 
sure why I didn't think of that before.

It's not entirely clear to me though how bytes.__new__ *can* set an 
object's value. Isn't __new__ also a regular function? Are these 
immutable classes special cases in the language that can't be recreated 
in the same way with user-defined classes? Not that that's something I 
want to do, and it's also not terribly important to me, but I'm trying 
to better understand what's going on.
> Best not to define an __init__ method at all, just use __new__.
>
> Something like:
>
> class BytesSubclass(bytes):
>      def __new__(cls, whatever, arguments, you, like):
>          bytesvalue = compute(whatever, arguments, you, like)
>          ob = bytes.__new__(cls, bytesvalue)
>          ob.some_other_att = compute_something_else(whatever, arguments, you, like)
>          return ob
Thanks, that works perfectly. That's also more important than 
understanding all the nitty-gritty details (I feel a basic understanding 
is important, but not necessarily always all the low-level details).

-- 
"There is no cause so noble that it will not attract fuggheads."
         -- Larry Niven