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. This creates an alias method
a() for the original method
Here’s a minimal example:
class Car: def change_oil(self): print('oil changed') def drive_to_cinema(self): print('movie watched') # Alias Method Names oil = change_oil cinema = drive_to_cinema # Create new car object porsche = Car() # Test original and alias method calls porsche.change_oil() # oil changed porsche.oil() # oil changed porsche.cinema() # movie watched porsche.drive_to_cinema() # movie watched
You create one
porsche. The original method
change_oil() may be too lengthy, so you decide to add an alias method to the class definition
oil = change_oil. Now, you can access the same method in two different ways:
porsche.change_oil() or simply
You can run this code interactively in our Jupyter Notebook:
Just click to the code and run it in a new tab!
Are Alias Methods Pythonic at All?
You should note, however, that using an alias at all is not very Pythonic! The Zen of Python clearly states that there should be one, and only one, way to accomplish a thing.
>>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!
What is Aliasing Anyway?
Aliasing happens if you access the same memory location via different symbolic names. If you change the data through one name, the other name pointing to the same data will see the change as well!
In the graphic, both variables
b point to the same imaginary object with value
42. You delete the object with
a.delete(). Now, both variables
b point to the empty object. Variable
b sees a change—even though it didn’t change anything!
“In computing, aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program. Thus, modifying the data through one name implicitly modifies the values associated with all aliased names, which may not be expected by the programmer. As a result, aliasing makes it particularly difficult to understand, analyze and optimize programs. Aliasing analysers intend to make and compute useful information for understanding aliasing in programs.” — Wikipedia
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.