0

There are at least two different ways to specify the alias of a function in Python. Given a function:

def fun1(a, b=5):
    return a+b

you could make an alias like this:

def fun2(*args, **kwargs):
    return fun1(*args, **kwargs)

or like this:

fun3 = fun1

The first creates a new function in memory, whereas the second is just a pointer to the original function. Is there any benefit to using one over the other?

Jeff
  • 12,147
  • 10
  • 51
  • 87
  • 1
    What do you need the alias for? – Klaus D. Dec 15 '19 at 03:12
  • @KlausD. backwards compatibility, semantic naming, etc.. – Jeff Dec 15 '19 at 19:47
  • So I guess it's fair to interpret your answer as: you don't really have a practical use-case for it. Unluckily the benefits you are asking for are highly dependent on a use case and its requirements. – Klaus D. Dec 15 '19 at 20:53
  • 1
    No, that is not correct at all, I provided two sepcific use cases. I author a software tool for academics in my area. As the tool has moved from personal use to broader adoption, I have used aliases to preserve backwards compatibility with older scripts, and to make function names more semantic (i.e., the field uses multiple names for the same concept and I'd like to have multiple names for the function). – Jeff Dec 15 '19 at 21:44

1 Answers1

-2

The first function very often is used in Python for decorating other functions. It is a "proxy function".

decorator

A function returning another function, usually applied as a function transformation using the @wrapper syntax. Common examples for decorators are classmethod() and staticmethod().

The decorator syntax is merely syntactic sugar, the following two function definitions are semantically equivalent:

   ...
f = staticmethod(f)

@staticmethod
def f(...):
    ...

The same concept exists for classes, but is less commonly used there. See the documentation for function definitions and class definitions for more about decorators.

That's very helpful for building clean code. More info at https://docs.python.org/3/glossary.html#term-decorator

The second one is just assigning a function to somewhere, variable, object or class property and etc. Usually is used in Monkey Patching

A monkey patch is a way for a program to extend or modify supporting system software locally (affecting only the running instance of the program).

The definition of the term varies depending upon the community using it. In Ruby, Python, and many other dynamic programming languages, the term monkey patch only refers to dynamic modifications of a class or module at runtime, motivated by the intent to patch existing third-party code as a workaround to a bug or feature which does not act as desired. Other forms of modifying classes at runtime have different names, based on their different intents. For example, in Zope and Plone, security patches are often delivered using dynamic class modification, but they are called hotfixes

More info at https://en.wikipedia.org/wiki/Monkey_patch

Alexandr Shurigin
  • 3,921
  • 1
  • 13
  • 25