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
Related Q & A:
- What Does Salvation Mean?Best solution by Yahoo! Answers
- What does it mean when a patent fee status has lapsed?Best solution by info.legalzoom.com
- What are the "points" all about, and what do they mean?Best solution by Quora
- What is a reconstructed title for a car, and what does it mean?Best solution by Yahoo! Answers
- What does it mean to be unresponsive? What's the difference between unresponsive and unconscious?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.