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

question about functions

P: n/a
Hi everyone.

I have a question about passing arguments to python functions. Is there
any way to make this job act like Perl?

sub my_funk {

print "the first argument: $_[0]\n";
print "the second argument: $_[1]\n"; }

In other words, can I call the arguments from a list?

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


P: n/a
Just pass a list. E.g.:

# start of ascript

def my_funk(alist):
print "the first argument: %s" % alist[1]
print "the second argument: %s" % alist[2]

mylist = ['wuzzup,','g?']

my_funk(mylist)

# end of ascript
Here is the output you expect:

the first argument: wuzzup,
the second argument: g?

On Thursday 14 April 2005 09:44 pm, chris patton wrote:
Hi everyone.

I have a question about passing arguments to python functions. Is there
any way to make this job act like Perl?

sub my_funk {

print "the first argument: $_[0]\n";
print "the second argument: $_[1]\n"; }

In other words, can I call the arguments from a list?


--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 19 '05 #2

P: n/a
Oops, I messed up the indices on the previous post.
Also, maybe you are thinking a bit more perlish:

# start of ascript

def my_funk(*alist):
print "the first argument: %s" % alist[0]
print "the second argument: %s" % alist[1]

my_funk('wuzzup,','g?')

# end of ascript

On Thursday 14 April 2005 09:44 pm, chris patton wrote:
Hi everyone.

I have a question about passing arguments to python functions. Is there
any way to make this job act like Perl?

sub my_funk {

print "the first argument: $_[0]\n";
print "the second argument: $_[1]\n"; }

In other words, can I call the arguments from a list?


--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jul 19 '05 #3

P: n/a
Am Freitag, 15. April 2005 06:44 schrieb chris patton:
In other words, can I call the arguments from a list?


Yes.
def testfunc(*args): ... print args[0]
... print args[1]
... testfunc("this is","a test")

this is
a test

Read up on positional and keyword arguments (the latter are something that'd
make me choose python over anything else instantly) in the Python tutorial
and documentation.

--
--- Heiko.
listening to: Pearl Jam - Given To Fly
see you at: http://www.stud.mh-hannover.de/~hwundram/wordpress/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBCX0tWf0bpgh6uVAMRAvQyAJ9OjvkkD9i78tS7JDqU48 cnKfaqdACeOEUx
OlRAXtd56fAlK4FAHmm0MLE=
=7tt0
-----END PGP SIGNATURE-----

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.