How to write Inline Functions in Python?

Brian Quinlan brian at sweetapp.com
Mon Nov 18 03:57:52 EST 2002


Michael Stenner wrote:
> I feel like we're going around in circles here.  I'm talking about
> python.  It looks like 'inline' would produce serious speedups in some
> cases.  Therefore, it looks like an inline command would not be
> useless.

Doing this would be almost impossible because Python name binding is
done at runtime. For example, how would you compile this?

if random.choice([0, 1]):
    from foo import baz # baz is an inline function
else:
    from bar import baz # baz is an inline function


def my_func():
    baz()

Or this?

inline def foo():
    ....

inline def bar():
    ....

def my_func()
    foo()
    globals['foo'] = bar
    foo()


Or this?

from bar import baz # baz is an inline function

def my_func()
    ....
    return baz # the fact that baz is inline isn't known at compile time

Not that an inline keyword should be added to the language even if it
were possible...

Cheers,
Brian





More information about the Python-list mailing list