[Tutor] typerror

spir denis.spir at free.fr
Fri Dec 11 15:59:01 CET 2009


"Roshan S" <roshan.s at live.com> dixit:

> class Student:
>      print"We have a new student "
>      def __init__(self,name='',credit=0,grade=0,quality=0):
>          self.name=name
>          self.credit=credit
>          self.grade=grade
>          self.quality=quality
> 
> 
>      def inputstudent(self):
>          self.name=raw_input("Enter student Name ")
>          self.credit=input("What da credit hours ")
>          self.grade=input("What da grade ")
> 
>      def quality(self):
>          self.quality=self.credit*self.grade
>          print"Quality Points: ",self.quality
> 
>      def average(self):
>          quality()
>          gpa=self.quality/self.credit
>          print"Grade point average: "+self.grade
>          if gpa == 4: print "Grade: A"
>          elif gpa == 3: print "Grade: B"
>          elif gpa == 2: print "Grade: C"
>          elif gpa == 1: print "Grade: D"
> 
>      def outputstudent(self):
>          "Name: "+self.name
> 
> #main
> #create new student
> stud1=Student()
> 
> #run teh method
> stud1.inputstudent()
> stud1.outputstudent()
> stud1.quality()
> stud1.average()
> 
> 
> RUN
> 
> 
> >>>
> We have a new student
> Enter student Name r
> What da credit hours 3
> What da grade 5
> Traceback (most recent call last):
>    File "C:\Users\Roshan\Desktop\no3.py", line 38, in <module>
>      stud1.quality()
> TypeError: 'int' object is not callable
> >>>
> 
> 
> PYTHON version 2.6.3, windows 7

You've got 2 attributes called 'quality'. One is a method that is defined on the class, the other a piece of data later defined on the student object itself at init time. The second one will override the method, hiding it. Python does not make a difference between behaviour attributes (methods) and state ones (data), so you cannot have a method called like a piece of data.
Anyway, in your case the method is an *action* that sets and prints out an attribute, so its name should reflect this fact.

Denis

________________________________

la vita e estrany

http://spir.wikidot.com/


More information about the Tutor mailing list