Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #197035
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Roel Schroeven <roel@roelschroeven.net> |
| Newsgroups | comp.lang.python |
| Subject | super().__init__() and bytes |
| Date | Tue, 3 Dec 2024 10:41:06 +0100 |
| Lines | 64 |
| Message-ID | <mailman.3.1733218873.2965.python-list@python.org> (permalink) |
| References | <3cc6272f-b151-474a-a83c-7f3339734bf5@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 4JtwqXMXHxnOy6yPmygOWQx2ardk31lIHcD58OWJ+fLQ== |
| Cancel-Lock | sha1:F9Pb1rclk6bWeBV04RzZYxPIslA= sha256:rmunwPNa12nU/ScWImT//qf0uNjBVmuFOZdefmoLbCQ= |
| 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=NGIV8tKZ; dkim-adsp=pass; dkim-atps=neutral |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'def': 0.04; 'traceback': 0.04; '(most': 0.05; 'class,': 0.05; 'last):': 0.05; 'achieved': 0.07; 'class.': 0.07; 'hopefully': 0.07; 'string': 0.07; '"hello': 0.09; 'apparently': 0.09; 'derived': 0.09; 'example:': 0.09; 'implicit': 0.09; 'int': 0.09; 'intelligent': 0.09; 'prints': 0.09; 'typeerror:': 0.09; 'import': 0.15; '(3)': 0.16; 'arguments': 0.16; 'call,': 0.16; 'cpython': 0.16; 'else?': 0.16; 'expected.': 0.16; 'initialize': 0.16; 'instance': 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; 'subject:() ': 0.16; 'subject:super': 0.16; 'whilst': 0.16; 'python': 0.16; 'to:addr:python-list': 0.20; 'skip:_ 10': 0.22; '\xe2\x80\x94': 0.22; 'version': 0.23; 'skip:p 30': 0.23; 'code': 0.23; '(and': 0.25; 'behavior': 0.26; 'done': 0.28; 'example,': 0.28; 'header:User-Agent:1': 0.30; 'takes': 0.31; "doesn't": 0.32; 'but': 0.32; "i'm": 0.33; 'windows': 0.34; 'same': 0.34; "it's": 0.37; 'class': 0.37; 'file': 0.38; 'use': 0.39; 'two': 0.39; "that's": 0.39; 'least': 0.39; 'base': 0.40; 'something': 0.40; 'want': 0.40; 'try': 0.40; 'method': 0.61; 'skip:o 10': 0.61; 'skip:\xc2 10': 0.62; 'ever': 0.63; 'skip:m 20': 0.63; 'skip:b 10': 0.63; 'time.': 0.66; 'lead': 0.67; 'exactly': 0.68; 'customize': 0.69; 'received:202': 0.69; 'man': 0.74; '8bit%:100': 0.76; 'implemented': 0.76; 'water': 0.81; 'happens': 0.84; 'dolphins': 0.84; 'adams': 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:message-id :mime-version:reply-to:subject:subject:to:to; s=fm3; t= 1733218868; x=1733305268; bh=NJp+ebLFf/T5NwBBykbxloTYrPUK03FQLmc jDTZBWXY=; b=NGIV8tKZsmy/M72c74gZi8BUHAJiRR6Jyp/6pkRKqF0GyiIoAoj jou42qqKC+aFG5qG31G3fdIpF/T0hp7vd3rCap2nWfzvmvAx1UnKCwz6axreZsgJ wesZ+9AWmw5gIFbE99yD12LzZy+Lp76vAmCvCQAx/5u1Q5/NnA4kEBF9Uo+7wqWn VP5zgqUcTBdVF1USOFlJviC9YBUJ+YuytU4wO9PZpkmH9Wj1S/k32LkPu0KPgjCi C5wKY7jtWMSl7w0MUyTSeHKN8PjIBTUlJKr8G6DZ4AZqmEvV41BWx9tEiZmR48Wh w3SDNaG3ISKTjFDGej3JIxSMOBkNeGpbJWQ== |
| 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:message-id:mime-version:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1733218868; x=1733305268; bh=NJp+ebLFf/T5NwBBykbxloTYrPUK03FQLmc jDTZBWXY=; b=UaAWr2SZI7HEZBKEe+b26eB4m7dGSA5JmN2SDkjxqlOh7lgN7Dt 50MPnScTNnVDSRTGqtEM7x0vbgcsfZhkRB1M7JcYQ0zKdtjMsAWjytAXWf+X35rJ HQ8LF6Q/KPAl2qf2GfN471JvNwm3K01esAYRiyBhPnCCI9LHvGRxh+sQrxlf8kot Zzhvu2SZpv09Nqbz+hrLIUKUGyMmHsvF0zULyk8FdeD25z433mocJir6TM5RtbIq rkm9qtrmSe+58b6IeC7Xjn6zLLNinEdH//OSsG0N84o04iLgZjlOJdPIrDA29A6p IXCfrFulxTLuAGcNG5Bi29S41+0bF6/1MUA== |
| X-ME-Sender | <xms:NNJOZ_fgRmN6oywPjfo3GMo1YGlpIsRZI4EwGVgnGZo7uuuLwnaqmQ> <xme:NNJOZ1PP9dCOMDii1iLsOETeKR_7jyHuK9L6TdJRWPgK-B-w0AdgGN1xRQPFhN2z7 sW9iVVdn4fb> |
| X-ME-Received | <xmr:NNJOZ4gw0uL5KGZzzpM7bDS3jaRh-armw2XAE3UYfuh3PDyVOlvW9x1jXlIcrD8zGBT3Y_lBtbFR5jetH8GYuHut2WH4AdnsaRzP_Zkzu73FaO8> |
| X-ME-Proxy-Cause | gggruggvucftvghtrhhoucdtuddrgeefuddriedvgddtjecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepkfffgggfvf fhufgtgfesthekredttddvjeenucfhrhhomheptfhovghlucfutghhrhhovghvvghnuceo rhhovghlsehrohgvlhhstghhrhhovghvvghnrdhnvghtqeenucggtffrrghtthgvrhhnpe egtdeltdevueelvdeukeejfeekueettddvueejfeeileehgfeiffdvgedtffdvffenucev lhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehrohgvlhesrh hovghlshgthhhrohgvvhgvnhdrnhgvthdpnhgspghrtghpthhtohepuddpmhhouggvpehs mhhtphhouhhtpdhrtghpthhtohepphihthhhohhnqdhlihhsthesphihthhhohhnrdhorh hg |
| X-ME-Proxy | <xmx:NNJOZw9GXF0RkNDfhqq7B7LEJ1e24Qklw0zJSIVTDCzZukmiPj_J3g> <xmx:NNJOZ7sSU3vTu2E7WPKy2CFUlebC2-s5B51aWahpYmL16sGUAZ6V8g> <xmx:NNJOZ_GXfRTvMxILKTfg3EqAGGVCH3uDf_saV6QIvA4aHrsOYhIKNg> <xmx:NNJOZyPJxRKvaO6-HNwmI0jOuk4Bm6sSI7NNsM87JOHhzsW28zvoiA> <xmx:NNJOZ0Vk6uS49-JPpeVBaahMNGaM2LtTqmZ5eY-wJfy6rViic--Z0_B3> |
| Feedback-ID | i8e5b41ae:Fastmail |
| User-Agent | Mozilla Thunderbird |
| Content-Language | nl, en-US |
| 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 | <3cc6272f-b151-474a-a83c-7f3339734bf5@roelschroeven.net> |
| Xref | csiph.com comp.lang.python:197035 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next | Find similar
super().__init__() and bytes Roel Schroeven <roel@roelschroeven.net> - 2024-12-03 10:41 +0100
csiph-web