1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ############################4 #File Name:class1.py5 #Author:frank6 #Mail: [email protected]7 #Created time:2017-09-04 13:35:278 ############################9 Ten #Self represents an instance of a class that represents the address of the current object, while Self.class points to a class. One #Self is not a python keyword, and we can do it properly if we change it to Self1. A classtestself: - defprt (self1): - Print(SELF1) the Print(Self1.__class__) - -t =testself () - t.prt () + - classEmployee: + 'base class of all employees' AEmpcount = 0#class Variables at - def __init__(Self, name, salary):#constructor Function -Self.name = Name#Self represents an instance of a class, and self is necessary when defining a method of a class, although you do not have to pass in the appropriate parameters when calling. -Self.salary =Salary -Employee.empcount + = 1 - in defDisplaycount (self): - Print("Total Employee%d"%employee.empcount) to + defDisplayemployee (self): - #print ("Name:", Self.name, ", Salary:", self.salary) the Print("name:%s, salary:%d"%(self.name,self.salary)) * $ #built-in properties of the classPanax Notoginseng Print("employee.__doc__:", Employee.__doc__) - Print("employee.__name__:", Employee.__name__) the Print("employee.__module__:", Employee.__module__) + Print("employee.__bases__:", Employee.__bases__) A Print("employee.__dict__:", Employee.__dict__) the +EMP1 = Employee (" One", 100) -EMP2 = Employee (" Both", 200) $ $ Emp1.displaycount () - Print("Total Employee%d"%employee.empcount) - Emp1.displayemployee () the Emp2.displayemployee () - Wuyi #Add, delete, modify properties of a class theEmp1.age=7 -Emp1.age=8 Wu delEmp1.age - About #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. - A PrintHasattr (EMP1,' Age')#returns True if the ' age ' property exists. + PrintSetAttr (EMP1,' Age', 1)#add attribute ' age ' value of 8 the PrintGetAttr (EMP1,' Age','Not find')#returns the value of the ' age ' property - PrintDelattr (EMP1,' Age')#Delete attribute ' age ' $ the #built-in properties of the class the Print("employee.__doc__:", Employee.__doc__) the Print("employee.__name__:", Employee.__name__) the Print("employee.__module__:", Employee.__module__) - Print("employee.__bases__:", Employee.__bases__) in Print("employee.__dict__:", Employee.__dict__)
Python-class (1)