functions with unlimeted variable arguments...

X

Xah Lee

how can i define a function with variable parameters? For example,

f(a) would return [a]
f(a,b) would return [a,b]
f(a,b,...) would return [a,b,...]

One solution is of course to make the argument as a list. i.e.
f([a,b,...])
but are there other solutions?

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
J

John Machin

Xah said:
how can i define a function with variable parameters? For example,

f(a) would return [a]
f(a,b) would return [a,b]
f(a,b,...) would return [a,b,...]

One solution is of course to make the argument as a list. i.e.
f([a,b,...])
but are there other solutions?
.... print repr(args)
.... return list(args)
....
()
[](42,)
[42]
(666, 'hello', 1.234)
[666, 'hello', 1.234]
 
I

Ivan Van Laningham

Hi All--

Paul said:
Geez man, haven't you been around long enough to read the manual?

def f(*a): return a

He's been around long enough to rewrite the manual.

Metta,
<if-he-succeeds-we'll-all-have-to-take-up-perl>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
God N Locomotive Works
http://www.andi-holmes.com/
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
 
X

Xah Lee

oops... it is in the tutorial... sorry.

though, where would one find it in the python reference?
i.e. the function def with variable/default parameters.

This is not a rhetorical question, but where would one start to look
for it in the python ref?

a language is used by programers. Subroutine definition with
variable/default parameters is a basic issue a programer wants to know,
and different languages differs very much in how they handle this. This
is what i mean that the language doc should be programing oriented, as
opposed to computer-sciency or implementation oriented...

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
R

Robert Kern

Xah said:
oops... it is in the tutorial... sorry.

though, where would one find it in the python reference?
i.e. the function def with variable/default parameters.

This is not a rhetorical question, but where would one start to look
for it in the python ref?

Oddly enough, http://docs.python.org/ref/function.html

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
D

Diez B. Roggisch

Xah said:
oops... it is in the tutorial... sorry.

though, where would one find it in the python reference?
i.e. the function def with variable/default parameters.

This is not a rhetorical question, but where would one start to look
for it in the python ref?
a language is used by programers. Subroutine definition with
variable/default parameters is a basic issue a programer wants to know,
and different languages differs very much in how they handle this. This
is what i mean that the language doc should be programing oriented, as
opposed to computer-sciency or implementation oriented...

Basically this boils down to you being to stupid to know the difference
between a library reference and a language reference.

And still you consider yourself to be above reading the tutorial in the
first place to grasp even the simplest aspects about python? Gosh, this
certainly isn't the first, but certainly one of the most enjoyable
moronities you've been presenting to us. When does it occur to you that
that type of moron-centric documentation you're bragging about is not
needed by anybody else than you?

So start writing "An introduction to Python for morons, by a moron - the
Xah Lee Files" yourself - take your time, apart from you nobody needs
it. Really.

And stop annoying people who are willing to help others seeking advice
who do show the will and abilities to actually dig into something and
learn instead of crying out in rage all the time because python (or it's
docs for that matter) doesn't seem to fit what your obviously severely
limited understanding of concepts behind programming and computers in
general tells you it should be.

But I have the feeling you won't restrain yourself from blathering away
in this NG - so thank god to killfiles...

Diez
 
P

Peter Hansen

Xah said:
oops... it is in the tutorial... sorry.

If you're sorry, have you now *finally* gone and worked through the rest
of tutorial, making a serious attempt to learn it?
This is not a rhetorical question, but where would one start to look
for it in the python ref?

a language is used by programers. Subroutine definition with
variable/default parameters is a basic issue a programer wants to know,
and different languages differs very much in how they handle this. This
is what i mean that the language doc should be programing oriented, as
opposed to computer-sciency or implementation oriented...

You seem to be under the impression that most programmers learn a new
language by looking up key phrases such as "variable argument list" in
the index of their new language's manual as they encounter the need for
a feature.

In actual fact, most programmers learn by finding a tutorial or
convenient beginner's lesson (hint hint), and then by browsing through
other material such as a FAQ (hint hint), and then perhaps by skimming
quickly through the reference material to learn what is there. This way
they avoid making themselves look like complete and utter fools by
appearing to learn only by looking up things in the index.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top