473,807 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem appending new class to a list of classes

13 New Member
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
Feb 15 '09 #1
13 3227
yueying53
13 New Member
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
Feb 15 '09 #2
bvdet
2,851 Recognized Expert Moderator Specialist
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.
Feb 15 '09 #3
yueying53
13 New Member
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, 637 views)
File Type: txt surveym.txt (6.3 KB, 448 views)
Feb 15 '09 #4
bvdet
2,851 Recognized Expert Moderator Specialist
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."
Feb 15 '09 #5
boxfish
469 Recognized Expert Contributor
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.
Feb 15 '09 #6
bvdet
2,851 Recognized Expert Moderator Specialist
Good post boxfish. I think yueying53 will get the message as well as a good explanation. :)
Feb 15 '09 #7
boxfish
469 Recognized Expert Contributor
Thanks, bvdet. By the way, you're getting close to 1337 posts.
Feb 15 '09 #8
bvdet
2,851 Recognized Expert Moderator Specialist
@boxfish
Ok, I'll take the bait. What is the significance of 1337 posts?

-BV
Feb 15 '09 #9
boxfish
469 Recognized Expert Contributor
It's cool to be 1337. Does this thread make it any clearer?
Feb 15 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
1360
by: Zhao Huang | last post by:
Hi, I'm new to python and I've come across a problem with my objects corrupting each other. class aclass: num = 0 l = ,] a = aclass()
5
1596
by: Nick | last post by:
I'm writing a simple class to represent a deck of cards. Here is the stripped down version. Notice classes Deck and Deck2. Class Deck works, Deck2 does not. My thinking is that it should be more efficient (a la Deck2) to pre-create the list, and then modify it, rather than appending Cards as in Deck. # --------------------snip-------------- class Card: def __init__(self, rank=0, suit=0): self.rank=rank
3
3154
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
5
2461
by: Leslaw Bieniasz | last post by:
Hello, I have the following OOP design problem. I have a base class designed as an element of a list, let's say: class A { public: // some stuff
5
1653
by: John Salerno | last post by:
Here's the code: class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY) panel = wx.Panel(self) dbconn = self.connect_db() dbconn.execute("select namefirst, namelast from master") bb_players =
16
1742
by: shapper | last post by:
Hello, I have a generic list as follows: Dim rows As New Generic.List(Of row) Now I have a row: Dim myRow As row I tried to check, further in my code, if the row is nothing: If myRow Is Nothing Then
3
1845
by: Mousam | last post by:
Hi All, First of all forgive me for the length of this post. I am developing one library for some text editor. In this library I want to provide a set of C++ classes which will enable client (of the library) to add tool bars to the text editor. The requirements are: -Client can add as many tool bars as he wants to add. -In each toolbar there will be some tools. Tools can be of button
10
15036
by: Debajit Adhikary | last post by:
I have two lists: a = b = What I'd like to do is append all of the elements of b at the end of a, so that a looks like: a =
0
1420
by: koren99 | last post by:
Hi, i am having trouble solving a design problem i'm sure is common, and wanted some help. for berevity i'll skim the problem to a smaller example. I have a design where a base class is called SEGMENT and inheriting from it is FLOW. in SEGMENT i keep a List Of BaseObjects, BaseObject is a Class which FlowObject Inherits From. the relationship should be:
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10628
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.