Python - Function argument with a comma [duplicate] - python

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')

Related

Require a tuple with a certain length in python function parameters [duplicate]

This question already has answers here:
Specify length of Sequence or List with Python typing module
(4 answers)
How to limit function parameter as array of fixed-size?
(3 answers)
Closed last month.
I am writing a function that needs to take in an ordered pair coordinate (represented in a tuple). The problem is that I don't know a way to require it to have exactly two elements in the parameters of the function. Is there a better way to handle this kind of situation (like a better data type to use)? I can't think of anything.
def function(coordinate: tuple):
pass
A tuple needs to be of a given length for some non-specific function to be executed properly.
We can check the number of elements in the tuple element by using the len() function on it that comes with python directly. Wrapped in a short if condition we can only execute the said function when we have guaranteed that the given tuple has 2 elements in it. Otherwise we may print a short error message for the user.
if len(your_tuple) == 2:
function(your_tuple)
else:
print("Please enter only 2D coordinates")

Pass list items as arguments to method [duplicate]

This question already has answers here:
How to extract parameters from a list and pass them to a function call [duplicate]
(3 answers)
How to split list and pass them as separate parameter?
(4 answers)
Closed 1 year ago.
Assuming i have a method definition like this do_stuff(*arg).
I have a list of values, ['a','b',...]. How best can i pass the values of this list as individual arguments to the do_stuff method as do_stuff('a','b',...) ?
I've unsuccessfully tried using list comprehension - do_stuff([str(value) for value in some_list])
I am hoping i don't have to change do_stuff() to accept a list
Any pointers would be appreciated.
You can pass them using the "*" operator. So an example would be like this:
def multiplyfouritems(a,b,c,d):
return a*b*c*d
multiplyfouritems(*[1,2,3,4])
It's the same syntax, with the *:
do_stuff(*['a','b','c'])

Trying to use multiple arguments for .append, anyway how? [duplicate]

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)]))

Can someone please explain to me the purpose of the asterisk in Python? [duplicate]

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.

What does **D mean in string.format(**D)? [duplicate]

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.

Categories