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?

<div><p class="has-pale-cyan-blue-background-color has-background">In contrast to a normal Python method, an <strong><em>alias method</em></strong> accesses an original method via a different name—mostly for programming convenience. An example is the iterable method <code>__next__()</code> that can also be accessed with <code>next()</code>. You can define your own alias method by adding the statement <code>a = b</code> to your class definition. This creates an alias method <code>a()</code> for the original method <code>b()</code>. </p>
<h2>Example Alias</h2>
<p>Here’s a minimal example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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
</pre>
<p>You create one <code>Car</code> object <code>porsche</code>. The original method <code>change_oil()</code> may be too lengthy, so you decide to add an alias method to the class definition <code>oil = change_oil</code>. Now, you can access the same method in two different ways: <code>porsche.change_oil()</code> or simply <code>porsche.oil()</code>.</p>
<h2>Interactive Notebook</h2>
<p>You can run this code interactively in our Jupyter Notebook:</p>
<figure class="wp-block-image size-large"><a href="https://colab.research.google.com/drive/1bFPdN0jExjbzXEdv5rsiqL8_dmg2r0uf?usp=sharing" target="_blank" rel="noopener noreferrer"><img loading="lazy" width="833" height="618" src="https://blog.finxter.com/wp-content/uploads/2020/12/image-41.png" alt="" class="wp-image-18601" srcset="https://blog.finxter.com/wp-content/uploads/2020/12/image-41.png 833w, https://blog.finxter.com/wp-content/uplo...00x223.png 300w, https://blog.finxter.com/wp-content/uplo...68x570.png 768w, https://blog.finxter.com/wp-content/uplo...50x111.png 150w" sizes="(max-width: 833px) 100vw, 833px" /></a></figure>
<p><em><strong>Just click to the code and run it in a new tab!</strong></em></p>
<h2>Are Alias Methods Pythonic at All?</h2>
<p>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.</p>
<pre class="wp-block-preformatted">>>> import this
<strong>The Zen of Python, by Tim Peters </strong> 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. <strong><em>There should be one-- and preferably only one --obvious way to do it.</em></strong> Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than <em>right</em> 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!</pre>
<h2>What is Aliasing Anyway?</h2>
<p>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!</p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="582" src="https://blog.finxter.com/wp-content/uploads/2020/12/image-43-1024x582.png" alt="Alias Example" class="wp-image-18606" srcset="https://blog.finxter.com/wp-content/uploads/2020/12/image-43.png 1024w, https://blog.finxter.com/wp-content/uplo...00x171.png 300w, https://blog.finxter.com/wp-content/uplo...68x436.png 768w, https://blog.finxter.com/wp-content/uplo...150x85.png 150w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>In the graphic, both variables <code>a</code> and <code>b</code> point to the same imaginary object with value <code>42</code>. You delete the object with <code>a.delete()</code>. Now, both variables <code>a</code> and <code>b</code> point to the empty object. Variable <code>b</code> sees a change—even though it didn’t change anything!</p>
<p><em>“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.</em>” — <a href="https://en.wikipedia.org/wiki/Aliasing_(computing)" title="https://en.wikipedia.org/wiki/Aliasing_(computing)" target="_blank" rel="noreferrer noopener">Wikipedia</a></p>
</p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>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?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>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.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
<p>The post <a href="https://blog.finxter.com/whats-the-most-pythonic-way-to-alias-method-names/" target="_blank" rel="noopener noreferrer">What’s the Most Pythonic Way to Alias Method Names?</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


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



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016