0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>In this lesson, we will look at how to combine different approaches when writing code. Also, we will cover common beginner errors.</p>
1
<p>In this lesson, we will look at how to combine different approaches when writing code. Also, we will cover common beginner errors.</p>
2
<p>Here we have the following code:</p>
2
<p>Here we have the following code:</p>
3
<p>It prints tirion on the screen.</p>
3
<p>It prints tirion on the screen.</p>
4
<p>All operations above are familiar, but we see several consecutive periods for the first time. This code combines the Python features that we currently know.</p>
4
<p>All operations above are familiar, but we see several consecutive periods for the first time. This code combines the Python features that we currently know.</p>
5
<p>It often happens in programming. When you do not know the syntax, you can combine different approaches. There is a chance they will work.</p>
5
<p>It often happens in programming. When you do not know the syntax, you can combine different approaches. There is a chance they will work.</p>
6
<p>Let us figure out how this code works. We need to break down the chain into separate operations:</p>
6
<p>Let us figure out how this code works. We need to break down the chain into separate operations:</p>
7
<p>The first and second examples are equivalent. We can choose from two options:</p>
7
<p>The first and second examples are equivalent. We can choose from two options:</p>
8
<ul><li>Perform operations sequentially by creating intermediate variables</li>
8
<ul><li>Perform operations sequentially by creating intermediate variables</li>
9
<li>Build a continuous chain of attributes and methods that goes from left to right, like computations in chains always do</li>
9
<li>Build a continuous chain of attributes and methods that goes from left to right, like computations in chains always do</li>
10
</ul><p>Here you see another example to get across the idea:</p>
10
</ul><p>Here you see another example to get across the idea:</p>
11
<p>This code needs a lot of thought. The .lower() function applies to the result of the method call, which is on the left. And the replace() method returns a string.</p>
11
<p>This code needs a lot of thought. The .lower() function applies to the result of the method call, which is on the left. And the replace() method returns a string.</p>
12
<p>Beginners often make mistakes in method chains and forget to place a call:</p>
12
<p>Beginners often make mistakes in method chains and forget to place a call:</p>
13
<p>Do not forget about the risk of building infinitely long and useless chains with slices:</p>
13
<p>Do not forget about the risk of building infinitely long and useless chains with slices:</p>
14
<p>It will not work with functions since programmers usually nest them inside each other - f(f(f())). It makes the analysis much more difficult. But it doesn't mean we cannot do it nicely. We can implement this feature in other languages through a function composition or a pipelining operator.</p>
14
<p>It will not work with functions since programmers usually nest them inside each other - f(f(f())). It makes the analysis much more difficult. But it doesn't mean we cannot do it nicely. We can implement this feature in other languages through a function composition or a pipelining operator.</p>