Why doesn't this work? (creating objects from a list)

Thread Solved

Join Date: Feb 2009
Posts: 143
Reputation: mahela007 is an unknown quantity at this point 
Solved Threads: 1
mahela007 mahela007 is offline Offline
Junior Poster

Why doesn't this work? (creating objects from a list)

 
0
  #1
Sep 6th, 2009
hi guys.
can anyone tell me why this code doesn't work? It doesn't raise any error but neither does it provide the expected results.

  1. my_list= ['a','b']
  2.  
  3. class my_class:
  4. def __init__ (self):
  5. self.name = ' my name is hi'
  6.  
  7. for a in my_list;
  8. a = my_class()
a.name() gives and error.
What do you mean I'm lazy? can't you see I'm trying so hard to do nothing?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,606
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Why doesn't this work? (creating objects from a list)

 
1
  #2
Sep 6th, 2009
Look, not to be harsh or anything, but given you previous posts I think you would be better off reading a book or a wiki or even just a tutorial on Python OOP and maybe Python in general. Really.
Last edited by scru; Sep 6th, 2009 at 3:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 143
Reputation: mahela007 is an unknown quantity at this point 
Solved Threads: 1
mahela007 mahela007 is offline Offline
Junior Poster

Re: Why doesn't this work? (creating objects from a list)

 
0
  #3
Sep 7th, 2009
I'm not trying to be a fully fledged programmer. I'm a hobbyist. So I don't want to buy any books on python. I have however, read a few tutorials on python
(a byte of python, how to think like a computer scientist, parts of the python documentation)... so why doesn't it (the code above) work?
Last edited by mahela007; Sep 7th, 2009 at 5:56 am.
What do you mean I'm lazy? can't you see I'm trying so hard to do nothing?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 151
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 46
snippsat snippsat is online now Online
Junior Poster

Re: Why doesn't this work? (creating objects from a list)

 
0
  #4
Sep 7th, 2009
so why doesn't it (the code above) work?
Why shoud it?
Break it thing down to understand what happends.
  1. >>> class my_class:
  2. def __init__ (self):
  3. self.name = ' my name is hi'
  4.  
  5. >>> a = my_class() #Class instance
  6. >>> print a
  7. <__main__.my_class instance at 0x011E23F0>
  8. >>>
  9. # This only show the class instance and memory location.
  10. # So you see now that your code can not work.
  11.  
  12. >>> a.name #call __init__(constructor)
  13. ' my name is hi'
  14.  
  15. >>> my_list= ['a','b']
  16. >>> for x in my_list:
  17. print a.name, x
  18. my name is hi a
  19. my name is hi b
  20.  
  21. This dosent make must sense.
  22.  
  23. Just one way that give a little more sense.
  24. ----------------
  25. my_list= ['a','b']
  26.  
  27. class my_class:
  28. def __init__ (self, name):
  29. self.name = name
  30.  
  31. a = my_class('Tom')
  32.  
  33. for x in my_list:
  34. print 'Hi %s my name is %s' % (a.name, x)
  35.  
  36. output-->
  37. Hi Tom my name is a
  38. Hi Tom my name is b
  39. ----------------
Last edited by snippsat; Sep 7th, 2009 at 9:39 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 143
Reputation: mahela007 is an unknown quantity at this point 
Solved Threads: 1
mahela007 mahela007 is offline Offline
Junior Poster

Re: Why doesn't this work? (creating objects from a list)

 
0
  #5
Sep 7th, 2009
OH- MY- GOD!!! The problem's been right under my nose. Thanks for your reply
What do you mean I'm lazy? can't you see I'm trying so hard to do nothing?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC