By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

functions with unlimeted variable arguments...

P: n/a
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
xa*@xahlee.org
http://xahlee.org/

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Xah Lee" <xa*@xahlee.org> writes:
but are there other solutions?

Xah


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

def f(*a): return a
Jul 19 '05 #2

P: n/a
Xah Lee wrote:
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?
def foo(*args): .... print repr(args)
.... return list(args)
.... foo() ()
[] foo(42) (42,)
[42] foo(666, 'hello', 1.234) (666, 'hello', 1.234)
[666, 'hello', 1.234]


Jul 19 '05 #3

P: n/a
Hi All--

Paul Rubin wrote:

"Xah Lee" <xa*@xahlee.org> writes:
but are there other solutions?

Xah


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/worksh...oceedings.html
Army Signal Corps: Cu Chi, Class of '70
Author: Teach Yourself Python in 24 Hours
Jul 19 '05 #4

P: n/a
Xah Lee wrote:
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
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.