This question already has answers here:
Why use **kwargs in python? What are some real world advantages over using named arguments?
(8 answers)
Use of *args and **kwargs [duplicate]
(11 answers)
Closed 8 years ago.
I am reading Programming Python and can't figure out what the **D mean in the following codes:
>>> D = {'say': 5, 'get': 'shrubbery'}
>>> '%(say)s => %(get)s' % D
'5 => shrubbery'
>>> '{say} => {get}'.format(**D)
'5 => shrubbery'
I googled **kwargs in python and most of the results are talking about to let functions take an arbitrary number of keyword arguments.
The string.format(**D) here doesn't look like something to let function take an arbitrary number of keyword arguments because I see the dictionary type variable D is just one argument. But what does it mean here?
Argument unpacking seems to be what you're looking for.
**D is used for unpacking arguments. It expands the dictionary into a sequence of keyword assignments, so...
'{say} => {get}'.format(**D)
becomes...
'{say} => {get}'.format(say = 5, get = shrubbery)
The **kwargs trick works because keyword arguments are dictionaries.
Short answer, I'm sure someone will come up with a dissertation later on.
**D here means that dictionary D will be used to fill in the "named holes" in the string format. As you can see, {say} got replaced by 5 and {get} got replaced by shrubbery.
Actually, it is the same mechanism as the one used for passing an arbitrary number of parameters to a function; format expects as many parameters as the "holes" in the string. If you want to wrap them up in a dictionary, that's how you do it.
For more information, check keyword arguments and unpacking, in Python's documentation, as Prashant suggested.
Related
This question already has an answer here:
Python3: What is the difference between keywords and builtins?
(1 answer)
Closed 3 years ago.
divmod seems to be a builtin as divmod(5,1) gives the correct tuple output with no prior imports (at least in Python 3.7.4).
On the other hand
import keyword
keyword.kwlist
does not list divmod. Also keyword.iskeyword('divmod') returns False. Actually even keyword.iskeyword('int') and keyword.iskeyword('hex') return false. Why is this? What is a keyword and where can we find the complete list of reserved string?
divmod is not a keyword, neither is int or hex. These are objects. You can tell by the fact that they are used with parenthesis to call (). Things like in and for and import are keywords.
This question already has answers here:
Which is the preferred way to concatenate a string in Python? [duplicate]
(12 answers)
Closed 4 years ago.
I am trying to append a set of objects combined into one as a single object on the end of a list. Is there any way I could achieve this?
I've tried using multiple arguments for .append and tried searching for other functions but I haven't found any so far.
yourCards = []
cards =["Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"]
suits = ["Hearts","Diamonds","Clubs","Spades"]
yourCards.append(cards[random.randint(0,12)],"of",suits[random.randint(0,3)])
I expected the list to have a new element simply as "Two of Hearts" etc. but instead I recieve this error:
TypeError: append() takes exactly one argument (3 given)
You are sending append() multiple arguments not a string. Format the argument as a string as such. Also, random.choice() is a better approach than random.randint() here as stated by: #JaSON below.
3.6+ using f-strings
yourCards.append(f"{random.choice(cards)} of {random.choice(suites)}")
Using .format()
yourCards.append("{} of {}".format(random.choice(cards), random.choice(suites)))
string concatenation
yourCards.append(str(random.choice(cards)) + " of " + str(random.choice(suites)))
#You likely don't need the str() but it's just a precaution
Improving on Alex's join() approch
' of '.join([random.choice(cards), random.choice(suites)])
yourCards.append(' '.join([random.choice(cards), "of", random.choice(suits)]))
This question already has answers here:
What does asterisk * mean in Python? [duplicate]
(5 answers)
How are python's unpacking operators * and ** used?
(1 answer)
Closed 4 years ago.
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.min(), urcrnrlat=lat.max())
x, y = m(*np.meshgrid(lat,lon))
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.
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.
This question already has answers here:
Can a variable number of arguments be passed to a function?
(6 answers)
Closed 4 years ago.
I want to have an argument in a function that fills an array with multiple strings, so that i have
def Test(colors):
colorarray = [colors]
which i can fill with
Test(red,blue)
but i always get either the takes 2 positional arguments but 3 were given error or the single strings do not get accepted (e.g. TurtleColor(Color[i]) tells me "bad color string: "red","blue")
i know i can pass the strings as seperate arguments, but i kind of want to avoid having that many different arguments.
You need to read input arguments as a list
def Test(*colors):
colorarray = [*colors]
print(colorarray)
Test('red','blue')
This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 8 months ago.
If I have a function:
def run(time, message, time_span_pattern):
...
And a list like:
run_args = ['1s', '1 second alarm', <_sre.SRE_Pattern object at 0x100435680>]
How can I pass the list, as separate arguments, to run? Is there a builtin way to do this, or am I forced to reference each element individually and by index?
You're looking for:
run(*run_args)
This is explained in more detail in this StackOverflow answer about the star and double star operator
It's also covered in the python docs