Skip to content Skip to sidebar Skip to footer

Can Someone Please Explain To Me The Purpose Of The Asterisk In Python?

For instance, can someone please explain to me the purpose of the asterisk in line 2 below? m = Basemap(projection='merc', llcrnrlon=lon.min(), urcrnrlon=lon.max(), llcrnrlat=lat.

Solution 1:

It means it will "expand" a collection in its individual elements.

So, suppose a function needs many arguments, and you have these arguments in a collection. Since you could not pass the collection itself (which would count as a single argument), you use the * so the collection is expanded and passed as its individual arguments to the function.

Solution 2:

from the documentation:

An asterisk * denotes iterable unpacking. Its operand must be an iterable. The iterable is expanded into a sequence of items, which are included in the new tuple, list, or set, at the site of the unpacking.

Post a Comment for "Can Someone Please Explain To Me The Purpose Of The Asterisk In Python?"