Hello there, again!

Well, once I solved the stupid bug I had in my first Python class ever, now I'm getting results I wasn't expecting.
The problem is: I instantiated two objects of this class but, apparently, one of the objects is just a copy of the other. And if I instantiate a third object (all different names), this one is also a copy of the first.
How come?
Please, could anyone tell me what's the catch?
What am I doing wrong?

Please see below the class and the test program:

# Square matrix

import random

class Matriz:
	"""Math operations with matrixes"""
	
	matriz = []
	grau_matriz = 0;

	def __init__ (self, grau_matriz, inicio):
	
		self.grau_matriz = grau_matriz
		
		#Decides if array will be populated with 0's or random numbers
		if inicio == 0:
			self.zero()
		elif inicio == 1:
			self.aleatoria()

	def zero(self):
		"""This method generates a N X N array with zeroes"""
		for i in range(self.grau_matriz):
			linha = []
			for j in range(self.grau_matriz):
				linha.append(0)
                
			self.matriz.append(linha)
			
	def aleatoria(self):
		"""This method generates a N X N array with random values"""
		for i in range(self.grau_matriz):
			linha = []
			for j in range(self.grau_matriz):
				linha.append(random.randrange( 1, 10 ))
                
			self.matriz.append(linha)

	def show(self):

		"""This method prints the array as an N x N format"""
		
		for i in range(self.grau_matriz):
			for j in range(self.grau_matriz):
				print "%2d " % self.matriz[i][j],
			print

		print

And the test program...

# Uses Matriz class

from Matriz import Matriz

matrix_a = Matriz(3,1) 
matrix_b = Matriz(3,0) 
matrix_c = Matriz(3,1) 

matrix_a.show()
print
matrix_b.show()
print
matrix_c.show()

When you make matriz global to the class it just keeps appending to it. Change your code this way:

# Square matrix

import random

class Matriz:
	"""Math operations with matrixes"""

	#matriz = []  # don't make this global!!!!
	grau_matriz = 0;

	def __init__ (self, grau_matriz, inicio):

		self.grau_matriz = grau_matriz

		#Decides if array will be populated with 0's or random numbers
		if inicio == 0:
			self.zero()
		elif inicio == 1:
			self.aleatoria()

	def zero(self):
		"""This method generates a N X N array with zeroes"""
		self.matriz = []
		for i in range(self.grau_matriz):
			linha = []
			for j in range(self.grau_matriz):
				linha.append(0)

			self.matriz.append(linha)

	def aleatoria(self):
		"""This method generates a N X N array with random values"""
		self.matriz = []
		for i in range(self.grau_matriz):
			linha = []
			for j in range(self.grau_matriz):
				linha.append(random.randrange( 1, 10 ))

			self.matriz.append(linha)

	def show(self):

		"""This method prints the array as an N x N format"""

		for i in range(self.grau_matriz):
			for j in range(self.grau_matriz):
				print "%2d " % self.matriz[i][j],
			print

		print


# test it ...
if __name__ == '__main__':
    matrix_a = Matriz(3,1)
    matrix_b = Matriz(3,0)
    matrix_c = Matriz(3,1)

    matrix_a.show()
    print
    matrix_b.show()
    print
    matrix_c.show()

Oh, I see! Thank you!
Now it works!

Thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.