Multiple argument passing

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 2,284
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 177
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Multiple argument passing

 
0
  #1
Oct 20th, 2006
How to I best pass multiple arguments to and from a function?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Multiple argument passing

 
0
  #2
Oct 20th, 2006
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".
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Multiple argument passing

 
0
  #3
Oct 20th, 2006
Yes, returning a tuple is considered the standard way to do it.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,070
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Multiple argument passing

 
1
  #4
Oct 20th, 2006
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]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,284
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 177
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Multiple argument passing

 
0
  #5
Oct 21st, 2006
Wow, Python makes multiple argument passing simple!
So, when you say:
  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:
  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?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Multiple argument passing

 
0
  #6
Oct 21st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,284
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 177
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Multiple argument passing

 
0
  #7
Oct 21st, 2006
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!?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Multiple argument passing

 
0
  #8
Oct 21st, 2006
Originally Posted by sneekula View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,070
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 938
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Multiple argument passing

 
0
  #9
Oct 21st, 2006
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]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC