1

i want to give multiple arguments to a functions from a string
for example if i have a function

def myfunc(x, y, z):
 return x*y+z

How do i give it arguments from a string like '1, 2, 3', I want something like this

def myfunc(x, y, z):
 return x*y+z
string = '1, 2, 3'
myfunc(string)
willeM_ Van Onsem
482k33 gold badges484 silver badges624 bronze badges
asked Mar 14, 2017 at 14:15
1
  • 1
    if a string is the argument you want to be passing to the function make the conversion inside the function rather than outside. Commented Mar 14, 2017 at 14:21

4 Answers 4

2

'1, 2, 3' is a string representation of a tuple of ints. A simple way to handle that is to use ast.literal_eval which allows you to "safely evaluate an expression node or a string containing ... the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None":

import ast
s = '1, 2, 3'
tup_of_int = ast.literal_eval(s) # converts '1, 2, 3' to (1, 2, 3)
myfunc(*tup_of_int) # unpack items from tup_of_int to individual function args

The code can be written on one line like so:

myfunc(*ast.literal_eval(s))

For more information about how the asterisk in myfunc(*tup_of_int) works see Unpacking Argument Lists from The Python Tutorial.

Note: Do not be tempted to eval in place of ast.literal_eval. eval is really dangerous because it cannot safely handle untrusted input, so you should not get comfortable using it.

answered Mar 14, 2017 at 14:22
Sign up to request clarification or add additional context in comments.

Comments

1

Replace the last line with myfunc(*map(int, string.split(','))).

string.split(',') splits the string into a list of the three arguments.

map(int, string.split(',')) converts the elements of the list to integers.

myfunc(*map(int, string.split(','))) splats the list so that the three elements of the list get passed as parameters.

This method relies on your input having the exact format as shown in your example string ('1,2,3' would not work). Steven's response is more robust so I recommend going with that one.

answered Mar 14, 2017 at 14:16

3 Comments

this passes strings to the function. You have to map them to integers or floats.
int will happily handle ' 2' (with a preceding space) so you can make your code more robust by splitting on a comma only instead of space+comma. This means that if the input becomes '1,2, 3' the code will still work.
@StevenRumbalski Thank you, I didn't realize int could parse the spaces. I've updated my answer.
0

Here is the code for your problem: First of all you have to split the string then proceed to next step:

def myfunc(x, y, z):
 return x * y + z
a=raw_input()
x,y,z=map(int,a.split(","))
print myfunc(x,y,z)
answered Apr 17, 2017 at 16:30

Comments

-1

One liner (although parsing string arguments like this could introduce vulnerabilities in your code).

>>> myfunc(*eval(string)) # myfunc((1, 2, 3))
5
answered Mar 14, 2017 at 14:33

2 Comments

Why recommend eval when ast.literal_eval exists? It's like recommending someone use a handgun for a flyswatter. Even with a disclaimer it's a bad idea.
Hmm, thought my disclaimer made it clear that this could be a potentially dangerous implementation. I think your statement is certainly fair. But to not show potential approaches at all seems like a self-imposed limitation to understanding a problem truly. @StevenRumbalski

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.