


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!

当我们在使用PHP进行开发时,有时会遇到”Tryingtogetproperty‘的解决方法’ofnon-object”的错误提示。这个错误的原因一般是因为程序中对一个不存在或者未实例化的对象进行访问,导致了PHP解析器无法识别该对象的属性或方法。那么,如何解决这个错误呢?下面我将为大家介绍几种可能的解决方法。一、检查代码首先,我们需要将出错的代

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version