Search
You exceeded the request by
0 characters

11. Calling methods by a chain in Python

The test was not passed
0
0

Summary

- In the previous lesson, we repeated the transformation and learned how to check which type the object belongs to.

- In the example, the split() method was used to split a string into a delimited list.

- Let's discuss the "chain method", which allows you to combine several intermediate variables into one operator.

- The method chain eliminates the additional variable and reduces the amount of code.

- The code may seem more difficult to understand when using the chaining method, so it is easier and clearer to create intermediate variables at the beginning of learning a language.

- The information in the lesson is provided for informational purposes, you can return to it as you learn the language.

In the previous lesson we repeated the conversion once again and learned how to check types. It is often necessary to perform several operations in a row with the data, for example:

Example (python)
str = "Hello, World"
l_str = str.split(',')
first_word = l_str[0]
s_first_word  = set(first_word)
c = s_first_word.__len__()
print(c) # => 4 

In this abstract example, using the split() method, which splits a string into a list where the separator is specified as a parameter, we get the first word, find which unique letters it consists of, and output their number. Do not pay attention to the essence of the example, now we need to deal with the so-called "chain method".

As you can see, we needed to create several intermediate variables to get the final result. But it can be avoided. Since all methods (and the set() function) return objects, then we can combine all these strings into one statement. Here is a simplified previous example:

Example (python)
set(str.split(',')[0]).__len__() # => 4        

The chain of methods eliminates an additional variable, which relieves the developer of the burden of remembering variables and significantly reduces the amount of code. In turn, the code may seem more difficult to understand.

At the very beginning of learning a language, you are unlikely to use this method, since it is easier and clearer to create intermediate variables.

In this lesson, the information was provided for informational purposes, as you learn the language, you can return to this lesson.

In the next lesson, we will summarize the results of the section «The very basics of Python» and then move on to to the section about loops and the random module.

Test task

Two seconds...
For the first time on the Codebra website?

Sorry about this pop-up, they annoy me too.

The codebra.ru educational resource is entirely dedicated to programming and computer security. All courses and lessons are on the main page. For the sake of interest, you can look at the content of courses on Active Directory Pentest, Python, HTML and CSS, JavaScript, C++ and others posted on the main page.

If you haven't found something, then use the site search, which is located on the main page at the very top.

Good luck learning!

Close the window