[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Few early questions on Class

joy99

3/3/2010 2:36:00 PM

Dear Group,

I was practising some early example of class if you can kindly let me
know where I was going wrong.

I am pasting directly from IDLE. There may be slight indentation
problem. I am using Python 2.6.4 on Windows XP Service Pack 3.

The Code and the output:

>>> class Student:
def _init_(self,name):
self.name=name
self.attendance=0
self.marks=[]
number_of_marks=len(self.marks)
print number_of_marks


>>> b=Student("Bobby")

Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
b=Student("Bobby")
TypeError: this constructor takes no arguments
>>> Class Shape:

SyntaxError: invalid syntax
>>> class Shape:
def _init_(self,x,y):
self.x=x
self.y=y
description="This Shape is not been described Yet"
author="Nobody has Claimed it Yet"
def area(self):
return self*x+self*y
def perimeter(self):
return 2*self.x+2*self*y


>>> rectangle=Shape(100,45)

Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
rectangle=Shape(100,45)
TypeError: this constructor takes no arguments
>>> rectangle=Shape()
>>> print rectangle.area()

Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
print rectangle.area()
AttributeError: Shape instance has no attribute 'area'
>>>
Best Regards,
Subhabrata.
1 Answer

jmg3000

3/3/2010 2:42:00 PM

0

Hi Subhabrata,

s/_init_/__init__/