Hướng dẫn dùng python asterisk python
There are a lot of places you’ll see Show
The If you’re newer to Python and you’re not yet familiar with keyword arguments (a.k.a. named arguments), I’d recommend reading my article on keyword arguments in Python first. What we’re not talking aboutWhen I discuss So I’m not talking about multiplication and exponentiation:
So what are we talking about?We’re talking about the
Two of the uses of This includes:
Even if you think you’re familiar with all of these ways of using Asterisks for unpacking into function callWhen
calling a function, the
That The Here’s another example:
Here we’re accepting a list of lists and returning a “transposed” list of lists.
The
From my experience, using Both Using
Using
You need to be careful when using Asterisks for packing arguments given to functionWhen defining a function, the
This function accepts any number of arguments:
Python’s The
That
Positional arguments with keyword-only argumentsAs of Python 3, we now have a special syntax for accepting keyword-only arguments to functions. Keyword-only arguments are function arguments which can only be specified using the keyword syntax, meaning they cannot be specified positionally. To accept keyword-only arguments, we can put named arguments after a
The above function can be used like this:
The arguments
This behavior was introduced to Python through PEP 3102. Keyword-only arguments without positional argumentsThat keyword-only argument feature is cool, but what if you want to require keyword-only arguments without capturing unlimited positional arguments? Python allows this with a somewhat strange
This function accepts an
But not like this:
This function accepts two arguments and one of them, I usually use keyword-only arguments while capturing any number of positional arguments, but I do sometimes use this Python’s built-in
There’s an Asterisks in tuple unpackingPython 3 also added a new way of using the The
If you’re wondering “where could I use this in my own code”, take a look at the examples in my article on tuple unpacking in Python. In that article I show how this use of the Usually when I teach
I’ve never seen a good use for this though and I don’t think I’d recommend using it even if you found one because it seems a bit cryptic. The PEP that added this to Python 3.0 is PEP 3132 and it’s not a very long one. Asterisks in list literalsPython 3.5 introduced a ton of new Say you have a function that takes any sequence and returns a list with the sequence and the reverse of that sequence concatenated together:
This function needs to convert things to lists a couple times in order to concatenate the lists and return the result. In Python 3.5, we can type this instead:
This code removes some needless list calls so our code is both more efficient and more readable. Here’s another example:
That function returns a new list where the first item in the given list (or other sequence) is moved to the end of the new list. This use of the This isn’t just limited to creating lists either. We can also dump iterables into new tuples or sets:
Notice that the last line above takes a list and a generator and dumps them into a new set. Before this use of
Double asterisks in dictionary literalsPEP 448 also expanded the abilities of
I wrote another article on how this is now the idiomatic way to merge dictionaries in Python. This can be used for more than just merging two dictionaries together though. For example we can copy a dictionary while adding a new value to it:
Or copy/merge dictionaries while overriding particular values:
Python’s asterisks are powerfulPython’s After reading about all the features of I tend to call these operators “star” and “double star” or “star star”. That doesn’t distinguish them from their infix relatives (multiplication and exponentiation), but context usually makes it obvious whether we’re talking about prefix or infix operators. If you don’t understand Practice makes perfectYou don’t learn by putting information in your head, you learn by attempting to retrieve information from your head. So you’ve just read an article on something new, but you haven’t learned yet. I highly recommend you write some code that you uses I’d love to send you an exercise on to get some practice with |