Skip to content

Commit 2333780

Browse files
committed
tests/cpydiff: Add diff for overriding __init__.
This adds a CPython diff that explains why calling super().__init__() is required in MicroPython when subclassing a native type (because __new__ and __init__ are not separate functions). Signed-off-by: David Lechner <david@pybricks.com>
1 parent 3543d9d commit 2333780

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
categories: Core,Classes
3+
description: When inheriting native types, calling a method in ``__init__(self, ...)`` before ``super().__init__()`` raises a ``RuntimeError`` (or segfaults if ``MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG`` is not enabled).
4+
cause: MicroPython does not have separate ``__new__`` and ``__init__`` methods in native types.
5+
workaround: Call ``super().__init__()`` first.
6+
"""
7+
8+
9+
class L1(list):
10+
def __init__(self, a):
11+
self.append(a)
12+
13+
try:
14+
L1(1)
15+
print("OK")
16+
except RuntimeError:
17+
print("RuntimeError")
18+
19+
20+
class L2(list):
21+
def __init__(self, a):
22+
super().__init__()
23+
self.append(a)
24+
25+
try:
26+
L2(1)
27+
print("OK")
28+
except RuntimeError:
29+
print("RuntimeError")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy