Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Let me put it this way: today is going to be a learning experience.


comp / comp.lang.python / Re: super().__init__() and bytes

SubjectAuthor
* Re: super().__init__() and bytesRoel Schroeven
`- Re: super().__init__() and bytesStefan Ram

1
Subject: Re: super().__init__() and bytes
From: Roel Schroeven
Newsgroups: comp.lang.python
Date: Tue, 3 Dec 2024 10:01 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: roel@roelschroeven.net (Roel Schroeven)
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>
View all headers

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

Subject: Re: super().__init__() and bytes
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Tue, 3 Dec 2024 10:26 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: super().__init__() and bytes
Date: 3 Dec 2024 10:26:33 GMT
Organization: Stefan Ram
Lines: 67
Expires: 1 Jan 2026 11:59:58 GMT
Message-ID: <__new__-20241203111801@ram.dialup.fu-berlin.de>
References: <mailman.4.1733220066.2965.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de W062Wbw8Jj/4MTfnS+O1UQ8UwYQi6zDNonE7S3kljIDoFP
Cancel-Lock: sha1:JWtXTReLsk4Tn/depNHGrwUuZzo= sha256:wjE+3AeUC+Y6dKQq9RPwFpGJL2yNP3dDts20axjIySQ=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
View all headers

Roel Schroeven <roel@roelschroeven.net> wrote or quoted:
>As a follow-up, it looks like this behavior is because bytes and int are
>immutable.

Alright, so you've stumbled onto some pretty gnarly behavior with
Python's immutable types. Let's unpack this burrito:

1) Here's the skinny on how it goes down with immutable types
like "bytes" and "int":

These bad boys do their thing in "__new__", not
"__init__". It's like trying to change a burrito after
it's been wrapped - ain't going to happen.

When you hit "MyBytes( b'abcdefghijlkmn' )", it's like this:

a) Python calls up "MyBytes.__new__".

b) That passes the buck to "bytes.__new__", which
whips up the new "bytes" object.

c) Then "MyBytes.__init__" gets a shot, but the horse
has already left the barn.

2) Wanna customize those args?

For immutable types, you got to override `__new__`.
It's like swapping out the ingredients before the
burrito's wrapped:

class MyBytes( bytes ):
def __new__( cls, data ):
return super().__new__( cls, data * 2 )

print( MyBytes( b'abc' )) # Spits out: b'abcabc'

3) Why "bytes" (and "int", ...) are different beasts:

You nailed it in your follow-up. It's all about being immutable.
These types are coded in C for speed, but the real kicker
is they can't change after creation.

On your other questions:

- Yeah, immutable types like "bytes" and "int" pretty much
ghost "__init__". All the magic happens in "__new__".

- There's definitely a connection between being immutable
and ditching "__init__". It's like setting your GPS
before you start driving - once you're moving, you can't
change the route.

- That "__init__" doc you quoted? It's legit for mutable
types, but for immutable ones, it's like trying to add
avocado to a sealed burrito - not going to work.

To wrap it up:

- Mutable types: Override "__init__" to set things up.

- Immutable types: "__new__" is where the party's at.

This setup lets Python keep its promises about immutability while
still letting you customize through subclassing. It's like having
your artisanal, gluten-free burrito and eating it too.

1

rocksolid light 0.9.8
clearnet tor