Article View: es.comp.os.linux.programacion
Article #7571Problema con clases Python
From: Jaime Suarez
Date: Sun, 16 Feb 2003 10:29
Date: Sun, 16 Feb 2003 10:29
41 lines
1964 bytes
1964 bytes
El c�digo de debajo, simplificado al m�ximo deber�a sumar dos polinomios. Sin embargo el resultado al ejecutarlo es: a= [[1, 2]] a+a= [[1, 4]] a= [[1, 4]] �Por qu� "a" se modifica? class polinomio: """ El polinomio se almacena como una lista de listas. Cada sublista contiene [grado, coeficiente] """ def __init__(self, lista=[[0,0]]): self.p = lista self.reducir() def reducir(self): """ Pone el polinomio en forma can�nica """ i = 0 while i < len(self.p)-1: # Reducir monomios semejantes if self.p[i][0] == self.p[i+1][0]: self.p[i][1] += self.p[i+1][1] del self.p[i+1] else: i = i + 1 def __add__(self, otro): """ Devuelve la suma de dos polinomios """ suma = polinomio(self.p + otro.p) # concatenar las dos listas return suma def __repr__(self): return str(self.p) a= polinomio([[1,2]]) print "a= ", a print "a+a= ", a+a print "a= ", a
Technical Details
Message-ID:
<slrnb4ttfr.2p0.jsuarez@cachonero.org>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.cs.ubc.ca!freenix!fr.usenet-edu.net!usenet-edu.net!news.completel.fr!kleenex.wirehub.nl!newsf01.ono.com!news.ono.com.POSTED!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII X-Google-Thread: f86c8,2c7eac70b8de8722,start X-Google-Attributes: gidf86c8,public X-Google-ArrivalTime: 2003-02-16 02:29:56 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.cs.ubc.ca!freenix!fr.usenet-edu.net!usenet-edu.net!news.completel.fr!kleenex.wirehub.nl!newsf01.ono.com!news.ono.com.POSTED!not-for-mail Newsgroups: es.comp.os.linux.programacion From: Jaime Suarez <jsuarez@ono.com> Subject: Problema con clases Python Organization: Corrupción de datos Reply-To: respuestas_al@grupo.news Message-ID: <slrnb4ttfr.2p0.jsuarez@cachonero.org> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit User-Agent: slrn/0.9.7.4 (Linux) Lines: 42 Date: Sun, 16 Feb 2003 10:29:48 GMT NNTP-Posting-Host: 81.203.221.129 X-Complaints-To: abuse@ono.com X-Trace: news.ono.com 1045391388 81.203.221.129 (Sun, 16 Feb 2003 11:29:48 MET) NNTP-Posting-Date: Sun, 16 Feb 2003 11:29:48 MET