I was reading the code of Python headq.merge and it seem like they're
creating alias for builtin function like _len = len
. Just wondering what's the purpose of that?
Thanks a lot!
In Python, aliasing happens whenever one variable's value is assigned to another variable, because variables are just names that store references to values.
In contrast to a normal Python method, an alias method accesses an original method via a different name—mostly for programming convenience. An example is the iterable method __next__() that can also be accessed with next() . You can define your own alias method by adding the statement a = b to your class definition.
The context is that they are assigning a global name to a local name inside the function:
def merge(*iterables):
...
_len = len
...
The expectation is that _len
will be used many times, and accessing a local name is faster than repeatedly looking up a global name. Whether
this makes a significant difference in the overall runtime can only be
determined by benchmarking your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With