Hacker News new | ask | show | jobs
by WoodTree 2377 days ago
Nesting functions like this can have a bad performance impact because functions in Python are objects. Normally, all of those function objects are instantiated once when you load the module. However nested functions will be instantiated at runtime every time their parent is called, even if they aren’t used. This cost is perceptible in hot paths.
3 comments

A lambda also behaves this way; it’s how it captures the closure scope.
Are lambdas different?
I’ve never seen it create a bottle neck, have you?
It will definitely be a problem if you use JIT interpreter like pypy