Hướng dẫn python asterisk before variable
Python has plentiful types of operations compared to other languages. Especially, the Asterisk(*) that is one of the most used operators in Python allows us to enable various operations more than just multiplying the two numbers. In this post, we’ll look at the various operations that can be done with this Asterisk(*) to write Python more pythonically. There are 4 cases for using the asterisk in Python.
Let’s look at each case. When used in multiplication and power operationsYou may already know of this case. Python supports the built-in power operations as well as multiplication. For repeatedly extending the list-type containersPython also supports that multiply the list-type container (includes tuple) and int for extending container data by given number times. For using the variadic argumentsWe often need variadic arguments (or parameters) for some functions. For example, we need it if we don’t know number of passing arguments or when we should process something with arbitrary passing arguments for some reasons. There are 2 kinds of arguments in Python, one is positional argumentsand other is keyword arguments, the former are specified according to their position and latter are the arguments with keyword which is the name of the argument. Before looking at the variadic positional/keyword arguments, we’ll talk about the positional arguments and keyword arguments simply. Above function has 2 positional arguments: Thus, what you can see here is that keyword arguments can be omitted, so they can not be declared before positional arguments. So, the following code will raises exceptions: But, in the third case, you can see that there are 3 positional arguments and 1 keyword
argument. Yes, for keyword arguments, if the passed position is the same to declared position, the keyword can be excluded and passed as positional arguments. That is, in above, the So far we’ve talked about the basic of arguments. By the way, one problem can be met here. The function can not handle the arbitrary numbers of runners because the function has fixed numbers of arguments. So we need the variadic arguments for it. Both positional arguments and keyword arguments can be used as variadic arguments. Let’s see following examples. When use only positional argumentsWhen use only keyword argumentsWhen use both positional arguments and keyword argumentsIn above, As you can see above, we are passing the arguments which can hold arbitrary numbers of positional or keyword values. The arguments passed as positional are stored in a tuple called As refered before, the keyword arguments can not be declared before positional arguments, so following code should raises exceptions: The variadic argument is very often used feature, it could be seen on many open source projects. Usually, many open sources use typically used argument names such as For unpacking the containersThe * can also be used for unpacking the containers. Its principles is similar to “For using the variadic arguments” in above. The easiest example is that we have data in the form of a list, tuple or dict, and a function take variable arguments: Because the For tuple, it could be done exactly same to list, and for dict, just use ** instead of *. And there is also one more type of unpacking, it is not for function but just unpack the list or tuple data to other variables dynamically. Here, the ConclusionSo far we’ve covered the Asterisk(*) of Python. It was interesting to be able to do various operations with one operator, and most of the those above are the basics for writing Pythonic code. Especially, the “For using the variadic arguments” is very important thing, but the python beginners often confused about this concept, so if you are a beginner of python, I would like you to know it better. Next, I’ll cover more interesting things about Python. Thank you. |