944,050 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 14775
  • Python RSS
Oct 20th, 2006
0

Multiple argument passing

Expand Post »
How to I best pass multiple arguments to and from a function?
Similar Threads
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Oct 20th, 2006
0

Re: Multiple argument passing

Here's one way:

[php]
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)
[/php]

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".
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 20th, 2006
0

Re: Multiple argument passing

Yes, returning a tuple is considered the standard way to do it.
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Oct 20th, 2006
1

Re: Multiple argument passing

Here is a case where you don't know exactly how many arguments you will be passing to a Python function:
[php]# 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
[/php]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 21st, 2006
0

Re: Multiple argument passing

Wow, Python makes multiple argument passing simple!
So, when you say:
Python Syntax (Toggle Plain Text)
  1. # or unpack into a tuple of appropriate variables
  2. tuple, sum, average = sum_average(2, 5, 6, 7)
It has to match the function's return arguments:
Python Syntax (Toggle Plain Text)
  1. 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?
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Oct 21st, 2006
0

Re: Multiple argument passing

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.
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 21st, 2006
0

Re: Multiple argument passing

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!?
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Oct 21st, 2006
0

Re: Multiple argument passing

Click to Expand / Collapse  Quote originally posted by sneekula ...
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.
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 21st, 2006
0

Re: Multiple argument passing

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 ...
[php]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
[/php]These kind of mistakes sneek up on you! If you accidentally use a Python keyword, the compiler lets you know right away ...
[php]# this gives a SyntaxError: invalid syntax
for = "don't use Python keywords for variable names"
[/php]If you are interested in the Python keywords ...
[php]print "A listing of all Python keywords:"
from keyword import kwlist
for kw in kwlist:
print kw
[/php]Should you be interested in avoiding builtins for variable names, look a this ...
[php]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
[/php]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Ideal project for a beginner?
Next Thread in Python Forum Timeline: password challenge





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC