Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Windows Tipps & SecurityThe Blood of Dawnwalker Director Says 60 FPS Is Enough for This RPG(23.09.2026 um 14:37 Uhr)
Windows Tipps & SecurityMeyer Sound ernennt John McMahon zum Chief Operating Officer(23.09.2026 um 11:19 Uhr)
Windows Tipps & SecurityTouchscreen erweitert Grill-Sortiment am Point of Sale(23.09.2026 um 13:45 Uhr)
Windows Tipps & Security„When Worlds Unite“: ISE 2027 erweitert Messe und Programm(23.09.2026 um 13:45 Uhr)
Sichere ProgrammierungWhat Full-Stack AI Engineering Means in Real Projects(23.09.2026 um 14:00 Uhr)
Sichere ProgrammierungThe Internet Changes Everything (Slowly)(23.09.2026 um 14:09 Uhr)
Sichere ProgrammierungMAUI vs React Native vs Flutter vs Ionic(23.09.2026 um 14:09 Uhr)
Sichere ProgrammierungAI Tools Used in Modern Software Development(23.09.2026 um 14:22 Uhr)
Sichere ProgrammierungHealthAuditor — Website Health & SEO Audit Tool(23.09.2026 um 14:24 Uhr)
Windows Tipps & SecurityThe Blood of Dawnwalker Director Says 60 FPS Is Enough for This RPG(23.09.2026 um 14:37 Uhr)
Windows Tipps & SecurityMeyer Sound ernennt John McMahon zum Chief Operating Officer(23.09.2026 um 11:19 Uhr)
Windows Tipps & SecurityTouchscreen erweitert Grill-Sortiment am Point of Sale(23.09.2026 um 13:45 Uhr)
Windows Tipps & Security„When Worlds Unite“: ISE 2027 erweitert Messe und Programm(23.09.2026 um 13:45 Uhr)
Sichere ProgrammierungWhat Full-Stack AI Engineering Means in Real Projects(23.09.2026 um 14:00 Uhr)
Sichere ProgrammierungThe Internet Changes Everything (Slowly)(23.09.2026 um 14:09 Uhr)
Sichere ProgrammierungMAUI vs React Native vs Flutter vs Ionic(23.09.2026 um 14:09 Uhr)
Sichere ProgrammierungAI Tools Used in Modern Software Development(23.09.2026 um 14:22 Uhr)
Sichere ProgrammierungHealthAuditor — Website Health & SEO Audit Tool(23.09.2026 um 14:24 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

@call_once python macro for unlimited recursion depth

I like competitive programming (like Meta Hacker Cup). Where you solve algorithmic puzzles in limited time. In many problems, a recursive solution feels more natural than an iterative one — but Python’s recursion depth limit (usually aro…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

I like competitive programming (like Meta Hacker Cup). Where you solve algorithmic puzzles in limited time. In many problems, a recursive solution feels more natural than an iterative one — but Python’s recursion depth limit (usually around 1000) makes it impractical for larger inputs.



So I built a small macro — @call_once — that lets you write recursive functions with virtually unlimited recursion depth.






Example



Let’s take a simple Fibonacci example:




def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)

print(fib(200_000) % 1_000)






This will crash immediately — Python’s call stack can’t handle that depth.


But with one decorator:




@call_once
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)

print(fib(200_000) % 1_000)






…it runs fine, limited only by memory.






Internals



To achieve unlimited recursion, we simulate the call stack in user space.


The macro rewrites the function’s AST into a continuation-style version.


For example, our fib becomes something like this:




def fib_aux(n):
if n <= 1:
return ('result', n)

fib_n_1_args = (n - 1,)
if fib_n_1_args not in fib_CACHE:
return ('call', fib_n_1_args)

fib_n_2_args = (n - 2,)
if fib_n_2_args not in fib_CACHE:
return ('call', fib_n_2_args)

return ('result', fib_CACHE[fib_n_1_args] + fib_CACHE[fib_n_2_args])






We do two main things:




  1. Check if recursive results are already cached.

  2. If not, return 'call' — meaning we need to compute that subcall.






Main loop



The wrapper function implements the “virtual stack”:




while stack:
current_args = stack.pop()
typ, value = fn(*current_args)
if typ == 'call':
stack.append(current_args)
stack.append(value)
continue
if typ == 'result':
cache[current_args] = value






Each frame is just a tuple of arguments.


If the inner call returns 'call', we push its arguments and continue.


This way, recursion happens in our heap-allocated stack, not Python’s C stack — so it never overflows.



The stack also behaves like an ordered set — the same arguments aren’t pushed twice, hence the name call_once.





Performance



To compare, let’s use an iterative Fibonacci:




def fib_fast(n):
lst = [0] * (n + 1)
lst[0] = 0
lst[1] = 1
for i in range(2, n + 1):
lst[i] = lst[i - 1] + lst[i - 2]
return lst[n]






Results (see fib.call_once.py):




fib_fast time:     1.10 seconds, result: 125
@call_once fib time: 1.29 seconds, result: 125






The overhead is small — within ~20%.


But it deserves deeper benchmarking on more complex recursion patterns.





Real-world use case



I first needed this macro during the Meta Hacker Cup 2025, in the problem A2: Snake Scales (Chapter 2).



Problem: facebook.com/codingcompetitions/hacker-cup/2025/round-1/problems/A2



You’re given a list of platform heights, e.g.:




4 2 5 6 4 2 1






Each number is the height of a platform.


You can climb between platforms using a ladder.


You can reach a platform either:




  • From the ground (ladder length ≥ platform height), or

  • From a neighbor (ladder length depending on the height difference).



The goal: find the minimal ladder length that allows visiting all platforms.





Recursive solution



We define a helper function min_left(n) — the minimal ladder length needed to reach platform n from the left or ground:




def min_left(n):
from_ground = A[n]
if n == 0:
return from_ground
dist = abs(A[n] - A[n - 1])
from_left = max(min_left(n - 1), dist)
return min(from_ground, from_left)






Then, compute the minimal ladder length overall:




mn = 0
for i in range(len(A)):
min_from_both = min(min_left(i), min_right(i))
mn = max(mn, min_from_both)






With @call_once, this works even for arrays of length 100_000 — still within time limits.



Full code: hackercup/a2.py






Summary



@call_once lets you write naturally recursive algorithms in Python without worrying about the recursion limit.


It works by rewriting your function into a manual stack loop that executes calls iteratively, preserving performance close to iterative code.



It’s ideal for competitive programming, graph traversals, dynamic programming, or any case where recursion feels right but Python says “RecursionError: maximum recursion depth exceeded”.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten @call_once python macro for unlimited recursion depth

Thematisch verwandte Begriffe: callonce, python, macro, unlimited · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-5695 | Arbitrary file upload vulnerability due to a lack of proper validation in…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick