Profiling slow imports in Python (jackevans.bearblog.dev)
from norambna@programming.dev to python@programming.dev on 23 Feb 2025 22:20
https://programming.dev/post/25920602

via fosstodon.org/@JackEvans/114055030506049316

#python

threaded - newest

wewbull@feddit.uk on 23 Feb 2025 22:45 next collapse

Why is the image on this story a paedobear?

norambna@programming.dev on 24 Feb 2025 00:20 collapse

LOL! It’s a blogging platform called Bear Blog: bearblog.dev

logging_strict@programming.dev on 24 Feb 2025 08:09 collapse

The sample code for lazy imports looks wrong

STRIPE = None

def _stripe():
    global STRIPE
    if STRIPE is None:
        import stripe

        return stripe
    return STRIPE

STRIPE is never changed. And two return statements in the same function?!

Anyways can imagine how to do lazy imports without relying on the given code sample.

MajinBlayze@lemmy.world on 24 Feb 2025 11:16 collapse

This is a function definition, not where it’s called; any number of things can happen between these statements running. Second, multiple return statements is not unusual when there is control logic, i.e. if statements.

logging_strict@programming.dev on 26 Feb 2025 15:01 next collapse

Multiple return statements is unusual. In very rare situations i understand. But the rule is never do that.

When there is only one return statement, can step into the function to see the local variables

logging_strict@programming.dev on 26 Feb 2025 15:21 collapse

You are in for a real treat!

Here is how to step in and get the locals

This technique depends on there being only one return statement

…readthedocs.io/…/context_locals.html