19 Python 3
发布时间:2023-04-15 14:11:21 所属栏目:教程 来源:
导读:Creating Instance Objects
To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts.
This would create first object of Employee
To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts.
This would create first object of Employee
|
Creating Instance Objects To create instances of a class, you call the class using class name and pass in whatever arguments its __init__ method accepts. This would create first object of Employee class emp1 = Employee("Zara", 2000) This would create second object of Employee class emp2 = Employee("Manni", 5000) Accessing Attributes You access the object's attributes using the dot operator with object. Class variable would be accessed using class name as follows − emp1.displayEmployee() emp2.displayEmployee() print ("Total Employee %d" % Employee.empCount) Now, putting all the concepts together − Live Demo #!/usr/bin/python3 class Employee: 'Common base class for all employees' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print ("Total Employee %d" % Employee.empCount) def displayEmployee(self): print ("Name : ", self.name, ", Salary: ", self.salary) #This would create first object of Employee class" emp1 = Employee("Zara", 2000) #This would create second object of Employee class" emp2 = Employee("Manni", 5000) emp1.displayEmployee() emp2.displayEmployee() print ("Total Employee %d" % Employee.empCount) When the above code is executed, it produces the following result − Name : Zara ,Salary: 2000 Name : Manni ,Salary: 5000 Total Employee 2 You can add, remove, or modify attributes of classes and objects at any time − emp1.salary = 7000 # Add an 'salary' attribute. emp1.name = 'xyz' # Modify 'age' attribute. del emp1.salary # Delete 'age' attribute. Instead of using the normal statements to access attributes, you can use the following functions − The getattr(obj, name[, default]) − to access the attribute of object. The hasattr(obj,name) − to check if an attribute exists or not. The setattr(obj,name,value) − to set an attribute. If attribute does not exist, then it would be created. The delattr(obj, name) − to delete an attribute. hasattr(emp1, 'salary') # Returns true if 'salary' attribute exists getattr(emp1, 'salary') # Returns value of 'salary' attribute setattr(emp1, 'salary', 7000) # Set attribute 'salary' at 7000 delattr(emp1, 'salary') # Delete attribute 'salary' Built-In Class Attributes Every Python class keeps following built-in attributes and they can be accessed using dot operator like any other attribute − __dict__ − Dictionary containing the class's namespace. __doc__ − Class documentation string or none, if undefined. __name__ − Class name. __module__ − Module name in which the class is defined. This attribute is "__main__" in interactive mode. __bases__ − A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list. For the above class let us try to access all these attributes − Live Demo #!/usr/bin/python3 class Employee: 'Common base class for all employees' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print ("Total Employee %d" % Employee.empCount) def displayEmployee(self): print ("Name : ", self.name, ", Salary: ", self.salary) emp1 = Employee("Zara", 2000) emp2 = Employee("Manni", 5000) print ("Employee.__doc__:", Employee.__doc__) print ("Employee.__name__:", Employee.__name__) print ("Employee.__module__:", Employee.__module__) print ("Employee.__bases__:", Employee.__bases__) print ("Employee.__dict__:", Employee.__dict__ ) When the above code is executed, it produces the following result − Employee.__doc__: Common base class for all employees Employee.__name__: Employee Employee.__module__: __main__ Employee.__bases__: (<class 'object'>,) Employee.__dict__: { 'displayCount': <function Employee.displayCount at 0x0160D2B8>, '__module__': '__main__', '__doc__': 'Common base class for all employees', 'empCount': 2, '__init__': <function Employee.__init__ at 0x0124F810>, 'displayEmployee': <function Employee.displayEmployee at 0x0160D300>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>, '__dict__': <attribute '__dict__' of 'Employee' objects> } (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
