[Tutor] A list of input arguments

Alan Gauld alan.gauld at btinternet.com
Tue Jan 13 10:16:58 CET 2009

"Mr Gerard Kelly" <s4027340 at student.uq.edu.au> wrote

>I have a problem with understanding how lists, strings, tuples, 
> types and input arguments all interact with each other.

OK, they are all different types. Some of them take a single value
(sometimes known as a scalar type) and others take a collection
of values (sometimes known as a sequence). Strings are unusual
in that they can be considered as a scalar type or as a collection
of characters!

Input parameters are simply local variables for the function to
which they are attached. Other than the fact that you can assign
them values from outside the function whe you call it they act just
like normal variables.

def f():
   print x

x = 42

Is the same (almost)) as

def f(x):
   print x

> As an example, if I use three arguments, it looks like this:
> def main():
>  play_for(waves(440,550,660), 5000)

> def main():
>  chord=[440,550,660]
>  play_for(waves(chord), 5000)
> it doesn't work.

Because you are passing a single value (a list) into a function that
expects 3 values.

> It doesn't work with a string or a tuple either.

Because strings and tuples are also single (container )entities

You must unpack your collection when calling the function:


> The problem is that another part of the code needs to take
> float(chord[0]), that is convert the first input value into the 
> float
> class, and the error message says "TypeError: float() argument must 
> be a
> string or a number."

Thats right, and as you did above you must extract the first element
since the code can't tell that yopu have passed it a collection 
of a number!

> Is there any way to list the input arguments without listing them 
> inside
> the function's parentheses?

No, because the function expects 3 arguments so you must pass it 3.
That is the contract (or interface) that exists betweeen the writer of 
function and you the consumer of it.  Programming. particularly on
larger projects with multiple teams, is all about defining interfaces
and adhering to them! If you want thebenefit of the function you must
respect its contract.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list