[Tutor] Objects in List

Viljoen, Danie danie.viljoen at siemens.com
Mon May 16 13:54:52 CEST 2005


Hi

I'm new to python (have a java background).  I'm trying to add object to a
list, and afterwards manipulate the object again.  How do a cast a object to
another object

My Code:

class MyObject:        
	"""A simple VO class"""
	def setName(self, newName):
		self.name=newName
		
	def getName(self):
		return self.name

def main():
	list=[]
	#created object in container
	for i in range(10):
    		myObject = MyObject()           
    		name = 'name:' + str(i)
    		myObject.setName(name)
    		list.append(myObject)
    	
    #manipulate object in list
	for p in enumerate(range(10)):
		myObject=p
    		print myObject.getName()

		
if __name__ == '__main__':
	main()

The ERROR:

C:\development\python__>python list.py
Traceback (most recent call last):
  File "list.py", line 25, in ?
    main()
  File "list.py", line 21, in main
    print myObject.getName()
AttributeError: 'tuple' object has no attribute 'getName'
    
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050516/9478a396/attachment.html


More information about the Tutor mailing list