browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need Python help?

Get answers from our community of Python experts on BYTES! It's free.

Problem appending new class to a list of classes

Newbie
 
Join Date: Dec 2008
Posts: 13
#1: Feb 15 '09
I am new to creating a list of classes. Hence I checked out this website: http://www.daniweb.com/code/snippet390.html. However, I modified the code to check if a class with a particular has been created before.

My code works when I create the first class and append it to the list. However, subsequent creations and appending results in this error: "AttributeError: No __call__ method defined." Why is this so? Why does the program from the above link work but mine doesn't? How do I solve the problem?

Below are snippets of my code. If you require any other parts of it for further understanding, feel free to let me know.

Thanks a million!

Expand|Select|Wrap|Line Numbers
  1. class survey:
  2.     def __init__(self,name,qn):
  3.         self.name = name
  4.         self.qn = qn
  5.         self.ans_list=[]    #array of integers
  6.         self.mean=''
  7.         self.sd=''
  8.         self.median=''
  9.         self.mode=[]    #array of string
  10.     def retrieve(self):
  11.         debug = 'Survey name: ' + self.name + '\r\n'
  12.         debug = debug + 'Qn: ' + self.qn + '\r\n'
  13.         return debug
Expand|Select|Wrap|Line Numbers
  1. survey_no=0
  2. surveylist=[]
  3. while (1):
  4.      if content_list[2].lower()=='create':
  5.          name = content_list[3].lower()    #SURVEY NAME
  6.          first_occur = content.find('"')+1
  7.          last_occur = content.rfind('"')
  8.          qn = content[first_occur: last_occur]    #SURVEY QUESTION
  9.          reply_sms=''
  10.          for survey in surveylist:
  11.              if survey.name==name:
  12.                  reply_sms = 'This survey already exists. '
  13.                  print reply_sms
  14.          if reply_sms=='':
  15.              surveylist.append(survey(name, qn))
  16.                  reply_sms = surveylist[survey_no].retrieve()
  17.              survey_no = survey_no + 1
  18.                      print reply_sms



Newbie
 
Join Date: Dec 2008
Posts: 13
#2: Feb 15 '09

re: Problem appending new class to a list of classes


Hi guys,

There's an error in my previous post. Lines 16 to 18 should be aligned according to line 15. Sorry for the error!

Regards,
Sam
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,708
#3: Feb 15 '09

re: Problem appending new class to a list of classes


I cannot test your code because it is incomplete. Try inheriting from object.

Expand|Select|Wrap|Line Numbers
  1. class survey(object):
Also, it may be helpful if you would post the error traceback which usually pinpoints the offending code.

Your problem may be in the for loop for survey in surveylist:. You are redefining the object reference survey.
Newbie
 
Join Date: Dec 2008
Posts: 13
#4: Feb 15 '09

re: Problem appending new class to a list of classes


Hi bvdet,

I've attached my full code, and this is the error traceback:

Expand|Select|Wrap|Line Numbers
  1. Traceback (innermost last):
  2.   File "C:\Program Files\Python\Pythonwin\pywin\framework\scriptutils.py", line 298, in RunScript
  3.     debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  4.   File "C:\Program Files\Python\Pythonwin\pywin\debugger\__init__.py", line 60, in run
  5.     _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  6.   File "C:\Program Files\Python\Pythonwin\pywin\debugger\debugger.py", line 582, in run
  7.     _doexec(cmd, globals, locals)
  8.   File "C:\Program Files\Python\Pythonwin\pywin\debugger\debugger.py", line 924, in _doexec
  9.     exec cmd in globals, locals
  10.   File "C:\Documents and Settings\Samantha Lim\Desktop\surveym.py", line 193, in ?
  11.     surveylist.append(survey(name, qn))
  12. AttributeError: no __call__ method defined
I'm quite new to this, so i'm not sure what you mean by "inheriting from object". I will go read up on it though. Meanwhile, if you happen to find out what exactly is wrong with my code, I will greatly appreciate some help.

Thank you so so much for all your help.

Regards,
Sam
Attached Files
File Type: txt sms.txt (1.7 KB, 54 views)
File Type: txt surveym.txt (6.3 KB, 45 views)
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,708
#5: Feb 15 '09

re: Problem appending new class to a list of classes


In your for loop for survey in surveylist:, you are redefining the class object survey as an instance of survey. Change the name of the class definitioin to Survey (capitalized).
Expand|Select|Wrap|Line Numbers
  1. class Survey(object):
Inheritance is a mechanism for creating a new class that builds on or modifies the behavior of an existing class. From Python 2.3 documentation:
"object( )

Return a new featureless object. object() is a base for all new style classes. It has the methods that are common to all instances of new style classes. New in version 2.2.
Changed in version 2.3: This function does not accept any arguments. Formerly, it accepted arguments but ignored them."
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#6: Feb 15 '09

re: Problem appending new class to a list of classes


Do what bvdet says; replace the line where you declare the survey class
Expand|Select|Wrap|Line Numbers
  1. class survey:
with
Expand|Select|Wrap|Line Numbers
  1. class survey(object):
so that the survey class inherits all the usual methods from the object class. Newer versions of Python will give you an error if you don't do this.

However, that is not the cause of your error. The problem is this line:
Expand|Select|Wrap|Line Numbers
  1. for survey in surveylist:
You are using the name of the survey class as the loop variable. Once it goes through this loop, it will think that every occurrence of the name survey means the counter variable, not the class. Call it something else, like i or eachSurvey or aSurvey or mySurvey or oneSurvey, and that should fix the error.
Expand|Select|Wrap|Line Numbers
  1. for oneSurvey in surveylist:
I hope this helps.

Edit:
Oops, bvdet got here first.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,708
#7: Feb 15 '09

re: Problem appending new class to a list of classes


Good post boxfish. I think yueying53 will get the message as well as a good explanation. :)
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#8: Feb 15 '09

re: Problem appending new class to a list of classes


Thanks, bvdet. By the way, you're getting close to 1337 posts.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,708
#9: Feb 15 '09

re: Problem appending new class to a list of classes


Quote:

Originally Posted by boxfish View Post

Thanks, bvdet. By the way, you're getting close to 1337 posts.

Ok, I'll take the bait. What is the significance of 1337 posts?

-BV
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#10: Feb 15 '09

re: Problem appending new class to a list of classes


It's cool to be 1337. Does this thread make it any clearer?
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,708
#11: Feb 16 '09

re: Problem appending new class to a list of classes


Makes perfect sense now.

-BV
Newbie
 
Join Date: Dec 2008
Posts: 13
#12: Feb 16 '09

re: Problem appending new class to a list of classes


Hi,

Thanks to the both of you for your explanations and help. I tried putting 'object' in like what you suggested. However, I've got this error:

Expand|Select|Wrap|Line Numbers
  1. Traceback (innermost last):
  2.   File "C:\Program Files\Python\Pythonwin\pywin\framework\scriptutils.py", line 298, in RunScript
  3.     debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  4.   File "C:\Program Files\Python\Pythonwin\pywin\debugger\__init__.py", line 60, in run
  5.     _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  6.   File "C:\Program Files\Python\Pythonwin\pywin\debugger\debugger.py", line 582, in run
  7.     _doexec(cmd, globals, locals)
  8.   File "C:\Program Files\Python\Pythonwin\pywin\debugger\debugger.py", line 924, in _doexec
  9.     exec cmd in globals, locals
  10.   File "C:\Documents and Settings\Samantha Lim\Desktop\surveym.py", line 7, in ?
  11.     class Survey(object):
  12. NameError: object
What does this mean? Btw, I'm using python that is built into a Telit GSM modem. They call it pythonWin. It has been giving some very different results from the original python. I wonder if this is the reason for the error above?

Thanks in advance!
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,708
#13: Feb 16 '09

re: Problem appending new class to a list of classes


Do this to check your Python version:
Expand|Select|Wrap|Line Numbers
  1. >>> import sys
  2. >>> sys.version
  3. '2.3.5 (#62, Feb  8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)]'
  4. >>> 
object was new to Python 2.2. You can eliminate the inheritance from object since that was not the source of your original error.
Newbie
 
Join Date: Dec 2008
Posts: 13
#14: Feb 16 '09

re: Problem appending new class to a list of classes


This was what my system returned: '1.5.2+ (#0, Oct 1 2004, 15:39:52) [MSC 32 bit (Intel)]'. I guess this version is just too old. =P

Anyway, I've solved the problem with my code (without the inheritance from object as u mentioned above) with the help from both you and boxfish. I've learnt a new thing about Python again. Thank you both so very much!

Regards,
Sam
Reply