Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] What’s the Most Pythonic Way to Alias Method Names?

#1
What’s the Most Pythonic Way to Alias Method Names?

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 b().

Example Alias


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 Car object 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 porsche.oil().

Interactive Notebook


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!

Alias Example

In the graphic, both variables a and b point to the same imaginary object with value 42. You delete the object with a.delete(). Now, both variables a and 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!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join the free webinar now!

The post What’s the Most Pythonic Way to Alias Method Names? first appeared on Finxter.



https://www.sickgaming.net/blog/2020/12/...hod-names/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] The Most Pythonic Way to Get N Largest and Smallest List Elements xSicKxBot 0 2,050 09-01-2023, 03:23 AM
Last Post: xSicKxBot
  [Tut] 5 Pythonic Ways to Print a List without Brackets xSicKxBot 0 1,365 05-25-2022, 10:57 PM
Last Post: xSicKxBot
  [Tut] Define A Method Outside Of The Class Definition xSicKxBot 0 1,268 04-07-2022, 05:23 PM
Last Post: xSicKxBot
  [Tut] Zip With List Output Instead of Tuple | Most Pythonic Way xSicKxBot 0 1,243 07-08-2020, 02:41 PM
Last Post: xSicKxBot
  [Tut] Create a List of Random Numbers — The Most Pythonic Way xSicKxBot 0 1,365 07-07-2020, 01:23 PM
Last Post: xSicKxBot
  [Tut] How to Sort a List of Tuples? – Most Pythonic Way! xSicKxBot 0 1,313 07-04-2020, 10:38 AM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Remove Multiple Items From a List xSicKxBot 0 1,342 06-29-2020, 01:37 AM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Compare Two Lists in Python xSicKxBot 0 1,378 06-28-2020, 01:18 PM
Last Post: xSicKxBot
  [Tut] List Difference | The Most Pythonic Way xSicKxBot 0 1,394 06-27-2020, 11:51 AM
Last Post: xSicKxBot
  [Tut] The Most Pythonic Way to Check if Two Unordered Lists Are Identical xSicKxBot 0 1,294 06-26-2020, 10:57 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016