Article View: chile.comp.programacion
Article #1173Re: funciones en python
From: Cristian Gutierrez
Date: Sat, 16 Dec 2006 04:17
Date: Sat, 16 Dec 2006 04:17
55 lines
2118 bytes
2118 bytes
Ayer en la tarde, David Az�car dijo: > Hola, quisiera hacer una pregunta > se puede hacer un arreglo de funciones en python? al estilo de c > > double (*BX[A])(double, double, double*, double*); > > donde BX[0] ser�a una funcion, BX[2] ser�a otra, etc. Es que debo definir > muchas funciones que son todas muy parecidas pero con ciertos par�metros > distintos, y no quiero hacerlas una por una, adem�s las debo meter en un > for, del tipo: > > for i in range(x): > BX[i]*5 > > etc. En Python (al igual que en Perl, y al contrario que en PHP o Java) las funciones son ciudadanos de primera clase: ,----[ funlist.py ] | def squared(x): | return x*x | | def cubic(x): | return x*x*x | | flist = [] | flist.append(squared) | flist.append(cubic) | | for x in [1, 2, 3, 4, 5]: | for f in flist: | print f(x), | print | `---- Esto imprime: ,---- | $ python funlist.py | 1 1 | 4 8 | 9 27 | 16 64 | 25 125 `---- Ojal� te sirva. Salu2, -- Guti "I object to all this sex on the television. I mean, I keep falling off!" -Monty Python
Technical Details
Message-ID:
<873b7gbhjw.fsf@guti.cl>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!news.vcd.cl!not-for-mail!news.cec.uchile.cl!not-for-mailReferences:
<elv0v5$678$1@localhost.localdomain>Show Full Headers
Complete Article Headers
X-Google-Thread: 1593cb,9b843d3029723c46 X-Google-Attributes: gid1593cb,public X-Google-Language: SPANISH,ASCII From: Cristian Gutierrez <crgutier@dcc.uchile.cl> Newsgroups: chile.comp.programacion Subject: Re: funciones en python Date: Sat, 16 Dec 2006 01:17:39 -0300 Organization: DCC/Uchile Lines: 55 Message-ID: <873b7gbhjw.fsf@guti.cl> References: <elv0v5$678$1@localhost.localdomain> NNTP-Posting-Host: vpn-client-022.cec.uchile.cl Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: localhost.localdomain 1166242662 6276 172.17.35.22 (16 Dec 2006 04:17:42 GMT) X-Complaints-To: news@localhost.localdomain NNTP-Posting-Date: Sat, 16 Dec 2006 04:17:42 +0000 (UTC) User-Agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:64uTPO8jYN3PtMKcXxGG/Lj8g+4= Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!news.kjsl.com!news.nivel5.cl!news.vcd.cl!not-for-mail!news.cec.uchile.cl!not-for-mail