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! - class survey:
-
def __init__(self,name,qn):
-
self.name = name
-
self.qn = qn
-
self.ans_list=[] #array of integers
-
self.mean=''
-
self.sd=''
-
self.median=''
-
self.mode=[] #array of string
-
def retrieve(self):
-
debug = 'Survey name: ' + self.name + '\r\n'
-
debug = debug + 'Qn: ' + self.qn + '\r\n'
-
return debug
-
survey_no=0
-
surveylist=[]
-
while (1):
-
if content_list[2].lower()=='create':
-
name = content_list[3].lower() #SURVEY NAME
-
first_occur = content.find('"')+1
-
last_occur = content.rfind('"')
-
qn = content[first_occur: last_occur] #SURVEY QUESTION
-
reply_sms=''
-
for survey in surveylist:
-
if survey.name==name:
-
reply_sms = 'This survey already exists. '
-
print reply_sms
-
if reply_sms=='':
-
surveylist.append(survey(name, qn))
-
reply_sms = surveylist[survey_no].retrieve()
-
survey_no = survey_no + 1
-
print reply_sms
13 3201
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 2,851
Expert Mod 2GB
I cannot test your code because it is incomplete. Try inheriting from 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.
Hi bvdet,
I've attached my full code, and this is the error traceback: - Traceback (innermost last):
-
File "C:\Program Files\Python\Pythonwin\pywin\framework\scriptutils.py", line 298, in RunScript
-
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
-
File "C:\Program Files\Python\Pythonwin\pywin\debugger\__init__.py", line 60, in run
-
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
-
File "C:\Program Files\Python\Pythonwin\pywin\debugger\debugger.py", line 582, in run
-
_doexec(cmd, globals, locals)
-
File "C:\Program Files\Python\Pythonwin\pywin\debugger\debugger.py", line 924, in _doexec
-
exec cmd in globals, locals
-
File "C:\Documents and Settings\Samantha Lim\Desktop\surveym.py", line 193, in ?
-
surveylist.append(survey(name, qn))
-
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
bvdet 2,851
Expert Mod 2GB
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).
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."
Do what bvdet says; replace the line where you declare the survey class
with
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: - 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. - for oneSurvey in surveylist:
I hope this helps.
Edit:
Oops, bvdet got here first.
bvdet 2,851
Expert Mod 2GB
Good post boxfish. I think yueying53 will get the message as well as a good explanation. :)
Thanks, bvdet. By the way, you're getting close to 1337 posts.
bvdet 2,851
Expert Mod 2GB @boxfish
Ok, I'll take the bait. What is the significance of 1337 posts?
-BV
It's cool to be 1337. Does this thread make it any clearer?
bvdet 2,851
Expert Mod 2GB
Makes perfect sense now.
-BV
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: - Traceback (innermost last):
-
File "C:\Program Files\Python\Pythonwin\pywin\framework\scriptutils.py", line 298, in RunScript
-
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
-
File "C:\Program Files\Python\Pythonwin\pywin\debugger\__init__.py", line 60, in run
-
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
-
File "C:\Program Files\Python\Pythonwin\pywin\debugger\debugger.py", line 582, in run
-
_doexec(cmd, globals, locals)
-
File "C:\Program Files\Python\Pythonwin\pywin\debugger\debugger.py", line 924, in _doexec
-
exec cmd in globals, locals
-
File "C:\Documents and Settings\Samantha Lim\Desktop\surveym.py", line 7, in ?
-
class Survey(object):
-
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 2,851
Expert Mod 2GB
Do this to check your Python version: - >>> import sys
-
>>> sys.version
-
'2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)]'
-
>>>
object was new to Python 2.2. You can eliminate the inheritance from object since that was not the source of your original error.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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()
|
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...
|
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...
|
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
|
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,...
|
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...
|
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...
|
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 =
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |