1. Class: used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
#!/usr/bin/python#-*-Coding:utf-8-*-Class Employee:#' base class for all employees 'Empcount= 0 Def__init__(Self,Name,Salary): Self.Name=NameSelf.Salary=SalaryEmployee.Empcount+= 1 DefDisplaycount(Self): Print "Total Employee%d" % Employee.EmpcountDefDisplayemployee(Self): Print("Name:", Self.Name, ", Salary:", Self.Salary# "Create first object of Employee class"Emp1= Employee("Zara", 2000) emp2 = employee ( "Manni" , 5000 emp1. () emp2. () print ( "total Employee%d" % Employee. Empcount)
Execute the above code to output the result as follows:
Name: Zara,Salary: +-name:Manni ,Salary: TotalEmployee2
2.
Emp1. =7# Add an ' age ' attribute emp1. =8# Modify the ' age ' property del emp1. # delete ' age ' property
- GetAttr (obj, name[, default]): Accesses the properties of the object.
- Hasattr (obj,name): Checks if a property exists.
- SetAttr (Obj,name,value): Sets a property. If the property does not exist, a new property is created.
- Delattr (obj, name): Deletes the attribute.
Hasattr(Emp1, ' age ' ) # if the existence of the ' age ' property returns TRUE. getattr (emp1, ' age ' ) # return the value of the ' age ' property setattr< Span class= "pun" > (emp1, ' age ' , 8) # Add attribute ' age ' value to 8delattr (empl ' age ' ) # delete attribute ' age '
Concept of the python-class and its use 1