How to I best pass multiple arguments to and from a function?

Recommended Answers

All 8 Replies

Member Avatar for Mouche

Here's one way:

def swap(x,y):
    a = y
    b = x
    return a,b

x = 3
y = 5

# prints "This is before the swap: x = 3; y = 5."
print "This is before the swap: x = %d; y = %d." % (x,y)

x,y = swap(x,y)

# prints "This is after the swap: x = 5; y = 3."
print "This is after the swap: x = %d; y = %d." % (x,y)

I'm a newer programmer, so this may not be the most efficient way.

Basically what happens is since you give two variables to assign the fuction to, the return values go into those respective variables. After that line is executed, the function's "a" will be the new "x" and the function's "b" will be the new "y".

Yes, returning a tuple is considered the standard way to do it.

Here is a case where you don't know exactly how many arguments you will be passing to a Python function:

# explore the argument tuple designated by *args
# used for cases where you don't know the number of arguments
def sum_average(*args):
    size = len(args)
    sum = 0
    for k in args:
        sum += k
    # return a tuple of three arguments
    return args, sum, sum/float(size)


print sum_average(2, 5, 6, 7)  # ((2, 5, 6, 7), 20, 5.0)

# or unpack into a tuple of appropriate variables
tuple, sum, average = sum_average(2, 5, 6, 7)

print "sum of %s = %d" % (tuple, sum)  # sum of (2, 5, 6, 7) = 20

print "average of %s = %0.2f" % (tuple, average)  # average of (2, 5, 6, 7) = 5.00

# or just pick one return value, here value at index 1 = the sum
print "sum =", sum_average(2, 5, 6, 7)[1]  # sum = 20
commented: always really helpful with python +1

Wow, Python makes multiple argument passing simple!
So, when you say:

# or unpack into a tuple of appropriate variables
tuple, sum, average = sum_average(2, 5, 6, 7)

It has to match the function's return arguments:

return args, sum, sum/float(size)

Also, do you return the argument tuple passed to the function just to aid in the display of the results?

Member Avatar for Mouche

Yes, he just added it in there so he could print the tuple easily. If you don't need to do that, that argument/return is unnecessary.

Thanks LaMouche, I thought so too! At least you know what args looks like, since it is in () and separated by commas, it must be a tuple.

One thing that bothers me, vegaseat used tuple for a variable name, isn't that a keyword in Python and therefore off limits!?

Member Avatar for Mouche

One thing that bothers me, vegaseat used tuple for a variable name, isn't that a keyword in Python and therefore off limits!?

Well. If you print it, you get "<type 'tuple'>." You get a value, but technically it's not a keyword such as "if" or "for." It is possible to assign a value to "tuple" and use it for your program, but it would be better to use a different name. Plus, there's probably a better variable to explain what you're using.

Here I keep telling folks not to use Python builtin function names for variable names, and I do it myself. Should have used something more descriptive like "args_tuple". (mildly red face!)

Here is a good reason why ...

str = "don't use Python built-in functions for variable names"

# now try to convert a number to a string using the built-in function str()
print str(7)  # TypeError: 'str' object is not callable

These kind of mistakes sneek up on you! If you accidentally use a Python keyword, the compiler lets you know right away ...

# this gives a SyntaxError: invalid syntax
for = "don't use Python keywords for variable names"

If you are interested in the Python keywords ...

print "A listing of all Python keywords:"
from keyword import kwlist
for kw in kwlist:
    print kw

Should you be interested in avoiding builtins for variable names, look a this ...

print "A listing of all Python builtin functions, classes and alike:"
# does a case insensitive sort first
for built_in in sorted(dir(__builtins__), key=str.lower):
    print built_in
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.