| | |
Why doesn't this work? (creating objects from a list)
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 143
Reputation:
Solved Threads: 1
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.
a.name() gives and error.
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)
my_list= ['a','b'] class my_class: def __init__ (self): self.name = ' my name is hi' for a in my_list; a = my_class()
What do you mean I'm lazy? can't you see I'm trying so hard to do nothing?
•
•
Join Date: Feb 2009
Posts: 143
Reputation:
Solved Threads: 1
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?
(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?
•
•
Join Date: Aug 2008
Posts: 151
Reputation:
Solved Threads: 46
•
•
•
•
so why doesn't it (the code above) work?
Break it thing down to understand what happends.
python Syntax (Toggle Plain Text)
>>> class my_class: def __init__ (self): self.name = ' my name is hi' >>> a = my_class() #Class instance >>> print a <__main__.my_class instance at 0x011E23F0> >>> # This only show the class instance and memory location. # So you see now that your code can not work. >>> a.name #call __init__(constructor) ' my name is hi' >>> my_list= ['a','b'] >>> for x in my_list: print a.name, x my name is hi a my name is hi b This dosent make must sense. Just one way that give a little more sense. ---------------- my_list= ['a','b'] class my_class: def __init__ (self, name): self.name = name a = my_class('Tom') for x in my_list: print 'Hi %s my name is %s' % (a.name, x) output--> Hi Tom my name is a Hi Tom my name is b ----------------
Last edited by snippsat; Sep 7th, 2009 at 9:39 am.
![]() |
Other Threads in the Python Forum
- Previous Thread: FFT Audio Analysis
- Next Thread: Take input and create a variable who's name is the input value
| Thread Tools | Search this Thread |
accessdenied advanced apache application argv array beginner book change command converter countpasswordentry csv dan08 def dictionary dynamic edit enter event examples file float format function google gui homework import inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric obexftp output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pygtk pyopengl pysimplewizard python random recursion redirect remote return reverse scrolledtext session simple skinning software sprite statictext string strings syntax terminal text threading time tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable voip wordgame wxpython






