Class
The class can be defined as a collection of objects. It is logical entity that has some specific attributes and methods. for example if you have an employee class then it should contain an attribute and method means an email id, name, age , salary etc.
Class is the representation of encapsulation.
Creating class in python
In python a class can be created by using the keyword class followed by the class name. Attributes are the variables that belongs to the class. Attributes are always public and can be accessed using the dot( . ) operator. like Myclass.Myattribute
Syntax
class ClassName :
statement(s)
A class contain a statement suite including fields, constructor, function etc.
Example
Consider the following example to create a class Employee which contains two fields as employee id and name. The class also contain a function display() which is used to display the information of the employee.
class Employee :
id=10
name=-"ayush"
def display(self) :
print(self.id,self.name)
Here is self is used as a reference variable which refers to the current class object. It is always the first argument in the function definition , however using self is optional in the function call.
The Pass statement
Class definition can not be empty but if you have some reason to have a class definition with no content put it in pass statement to avoid getting an Error.
Example
class Person :
pass
No comments:
Post a Comment