943,922 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 571
  • Python RSS
Sep 6th, 2009
0

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

Expand Post »
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.

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 16
Solved Threads: 2
Posting Whiz in Training
mahela007 is offline Offline
214 posts
since Feb 2009
Sep 6th, 2009
1

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

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.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Sep 7th, 2009
0

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

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.
Reputation Points: 16
Solved Threads: 2
Posting Whiz in Training
mahela007 is offline Offline
214 posts
since Feb 2009
Sep 7th, 2009
0

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

Quote ...
so why doesn't it (the code above) work?
Why shoud it?
Break it thing down to understand what happends.
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is offline Offline
771 posts
since Aug 2008
Sep 7th, 2009
0

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

OH- MY- GOD!!! The problem's been right under my nose. Thanks for your reply
Reputation Points: 16
Solved Threads: 2
Posting Whiz in Training
mahela007 is offline Offline
214 posts
since Feb 2009
Sep 19th, 2011
0
Re: Why doesn't this work? (creating objects from a list)
This was not a real thread... I'm going to go cry
Reputation Points: 34
Solved Threads: 19
Posting Whiz
pyguy62 is offline Offline
345 posts
since Aug 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: raw_input
Next Thread in Python Forum Timeline: Image from askopenfilename() not showing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC