| | |
Multiple argument passing
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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".
[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".
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]
[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!
Wow, Python makes multiple argument passing simple!
So, when you say:
It has to match the function's return arguments:
Also, do you return the argument tuple passed to the function just to aid in the display of the results?
So, when you say:
Python Syntax (Toggle Plain Text)
# or unpack into a tuple of appropriate variables tuple, sum, average = sum_average(2, 5, 6, 7)
Python Syntax (Toggle Plain Text)
return args, sum, sum/float(size)
No one died when Clinton lied.
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 ...
[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]
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!
![]() |
Similar Threads
Other Threads in the Python Forum
- Previous Thread: Ideal project for a beginner?
- Next Thread: password challenge
| Thread Tools | Search this Thread |
ansi assignment avogadro backend beginner binary bluetooth character cmd code copy customdialog cx-freeze data decimals dictionary directory drive dynamic error examples excel exe file float format function gnu graphics gui halp heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib






