Few early questions on Class

joy99 subhakolkata1234 at gmail.com
Wed Mar 3 15:36:23 CET 2010


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.



More information about the Python-list mailing list