Home  >  Article  >  Backend Development  >  How to access object attributes in python? Two methods instance analysis

How to access object attributes in python? Two methods instance analysis

乌拉乌拉~
乌拉乌拉~Original
2018-08-20 14:38:233844browse

In this article, let’s learn about the knowledge about objects in python. Some friends may have just come into contact with the programming language python and have less understanding of python objects. I don’t know much about python object-oriented. It's not clear how the object's properties should be accessed. In the next article, we will learn how to access the properties of python objects.

Access properties

You can use the period . to access the properties of an object. Use the name of the following class to access class variables:

emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount

Next let’s look at a complete example:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
class Employee:
   '所有员工的基类'
   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
 
"创建 Employee 类的第一个对象"
emp1 = Employee("Zara", 2000)
"创建 Employee 类的第二个对象"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print "Total Employee %d" % Employee.empCount

The output result of executing the above code is as follows:

Name :  Zara ,Salary:  2000
Name :  Manni ,Salary:  5000
Total Employee 2

You can add , delete and modify the attributes of the class, as shown below:

emp1.age = 7  # 添加一个 'age' 属性
emp1.age = 8  # 修改 'age' 属性
del emp1.age  # 删除 'age' 属性

You can also use the following function to access the attributes:

1.getattr(obj, name [, default]): access the properties of the object.

2.hasattr(obj,name): Check whether an attribute exists.

3.setattr(obj,name,value): Set an attribute. If the property does not exist, a new property is created.

4.delattr(obj, name): Delete attributes.

hasattr(emp1, 'age')    # 如果存在 'age' 属性返回 True。
getattr(emp1, 'age')    # 返回 'age' 属性的值
setattr(emp1, 'age', 8) # 添加属性 'age' 值为 8
delattr(emp1, 'age')    # 删除属性 'age'

The above is all the content of this article. This article mainly introduces python’s knowledge about objects. I hope you can use the information to understand what is said above and the examples given. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of How to access object attributes in python? Two methods instance analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more