rishyak
rishyak

Reputation: 15

Set multiple names for one function

I am writing a python object class. I just realised that a bunch of my functions are in UK English instead of US English and I need to make sure others on my team can use it.

I was wondering if I can set multiple names for one function so people familiar with both versions of English can use the product.

For example:

def analyse(a, b, c):
    pass

def analyze(a,b,c):
    return alalyse(a, b, c)

Upvotes: 1

Views: 1238

Answers (5)

Corralien
Corralien

Reputation: 120409

All answers are right but I prefer an approach where the user understands the "problem":

import warnings

def analyse(a, b, c):
    pass

def analyze(*args, **kwds):
    """Use 'analyse' and not 'analyze'."""
    warnings.warn(analyze.__doc__)
    return analyse(*args, *kwds)
>>> analyze(1, 2, 3)
<ipython-input-125-fcac1ea67ac0>:5: UserWarning: Use 'analyse' and not 'analyze'.
  warnings.warn(analyze.__doc__)
>>> help(analyze)
Help on function analyze in module __main__:

analyze(*args, **kwds)
    Use 'analyse' and not 'analyze'.

With analyze = analyse

>>> help(analyze)
Help on function analyse in module __main__:

analyse(a, b, c)
#    ^ ???

Upvotes: 1

enzo
enzo

Reputation: 11496

You can also use a decorator!

import functools

def alias(alias_f):
    def _(_):
        @functools.wraps(alias_f)
        def _(*args, **kwargs):
            return alias_f(*args, **kwargs)
        return _
    return _

Then use it like that:

def analyze(x):
    return f"Analyzing {x}"
    
@alias(analyze)
def analyse(x): 
    pass

print(analyze(42))    # Outputs Analyzing 42
print(analyse(42))    # Outputs Analyzing 42

Upvotes: 0

chepner
chepner

Reputation: 531135

As functions are first-class objects, you can assign names to them like any other object.

>>> def analyse(a, b, c):
...     print("analysis complete")
...
>>> analyze = analyse
>>> analyze(1,2,3)
analysis complete

A def statement itself is a fancy assignment statement. It defines an otherwise anonymous object, then binds a name to that object.

As far as Python is concerned, there's no difference between the names analyze and analyse: both refer to the same object, and neither one is considered the "real" name. (The fact that analyze.__name__ is "analyse" is a side effect of the def statement, nothing more. The __name__ attribute is metadata that is independent of any name referring to the object, and can be changed at will.)

Upvotes: 0

Kache
Kache

Reputation: 16677

Functions can be set to local variables as well:

def foo(x):
  return x

bar = foo

bar(5) == 5

Though I'm personally a fan of aliasing when it can notably improve expressiveness & readability, alternative opinions point to The Zen of Python:

There should be one-- and preferably only one --obvious way to do it.

Your bar for "notable improvement of expressiveness" should be quite high, as aliases hinder findability & traceability, and can subvert expectations. End-user localization is ideally decoupled from app logic specifically to address that kind of problem.

As a developer, I already have to spend a lot of time learning the non-programming, business-domain related concepts pertinent to what I'm building. I wouldn't mind at all to adopt slight variations of British/American/whatever language in return for unambiguous code.

Upvotes: 1

ThePyGuy
ThePyGuy

Reputation: 18416

In Python everything is an object, so you can just assign the function to a variable which will create a named reference for the function object:

def analyse(a, b, c):
    pass

analyze = analyse
analyze(1,2,3)

Upvotes: 0

Related Questions