What does {!r} mean in Python?

What does the '*' mean in python?

  • In the following code for example, what does '*' mean? def cheeseshop(kind, *arguments, **keywords): print "-- Do you have any", kind, "?" print "-- I'm sorry, we're all out of", kind for arg in arguments: print arg print "-" * 40 keys = sorted(keywords.keys()) for kw in keys: print kw, ":", keywords[kw]

  • Answer:

    Python provides a functionality if we want to pass arbitrary number of arguments. The star(*) in definition of the function will accept the arguments supplied in the function that are not identified as a list. Although if you have identifier for an argument they must be in order. First the identifier then (*) or (**). #This will return sum of all the arguments you provide. def sum_all(first, *others): return first + sum(others) print sum_all(0, 1, 2, 3, 4) #sum = 0 and prints 10 The same way if we pass the arguments with identifying definition, it can be taken in the dictionary using double stars(**). #Same sum function as above. def sum_all(sum, **others): for key in others: sum += others[key] return sum print sum_all(0, one=1, two=2) #prints 3, one and two will become keys for the argument others. Here more about keyword arguments in Python. http://docs.python.org/2/tutorial/controlflow.html#keyword-arguments

Neeraj Khandelwal at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.