[Tutor] A list of input arguments
Mr Gerard Kelly
s4027340 at student.uq.edu.au
Tue Jan 13 12:32:52 CET 2009
Many thanks for your helpful answer Alan.
My only other question is, does there exist a convenient way to unpack a
collection of variable length?
If you know that there are 3 elements in the collection, you can specify:
play_for(waves(chord[0],chord[1],chord[2]),500)
But then if you want to change the number of elements in the list
"chord", say from 3 to 5, you have to change the code accordingly. Is
there some sort of way to instruct the code to unpack its elements for
any number of elements?
Like
i=len(chord)
play_for(waves(chord[0],chord[1],...,chord[i-1]),500)
If there is a way to do this it would be great!
----- Original Message -----
From: Alan Gauld <alan.gauld at btinternet.com>
Date: Tuesday, January 13, 2009 7:16 pm
Subject: Re: [Tutor] A list of input arguments
> "Mr Gerard Kelly" <s4027340 at student.uq.edu.au> wrote
>
> >I have a problem with understanding how lists, strings, tuples,
> >number
> > 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
> f()
>
> 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:
>
> play_for(waves(chord[0],chord[1],chord[2]),5000)
>
> > 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
> instead
> 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
> the
> 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.
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list