Example HTML page

Creating Classes

I am going to discuss about how to create classes in python programming.You might be wondered, how easy it is to create a class and objects for the class in python when compared to Java and C++ programming languages. 

Creating Classes:

A class can be created by using a “class” keyword followed by a colon. The following is the syntax for creating classes.

Syntax

class ClassName:
             ‘class documentation string’                   (optional)
           class_suite

Ex

>>> class Student:
	"An example for creating a 'Student' class"

	
>>> Student
<class '__main__.Student'>

If you want to find the description about the class ‘Student’ we use the following:

>>> Student.__doc__

“An example for creating a ‘Student’ class”

Here “__.doc__” is a built in attribute in python which is used to access the information in that class.

The following is an example of simple class.

>>> class SimClass:
	"An example sample class"
	Z=2,5,8


	

To make sure that the class is created or not, then

>>> SimClass

<class ‘__main__.SimClass’>

To access the information that exists in ‘SimClass’ then

>>> SimClass.__doc__

‘An example sample class’

To access the elements that exists in ‘SimClass’ then

>>> SimClass.Z

(2, 5, 8)

To create an object for the ‘SimClass’, then

>>> y=SimClass()

>>> y

<__main__.SimClass object at 0x0000000003557748>

To access the elements of ‘SimClass’ through the created object, then

>>> y.Z

(2, 5, 8)

To replace one element with the other, then

>>> y.Z=10

>>> y.Z

10

 

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest