Home Backend Development Python Tutorial Python入门篇之对象类型

Python入门篇之对象类型

Jun 06, 2016 am 11:19 AM
python getting Started Object type

Python使用对象模型来存储数据。构造任何类型的值都是一个对象

所有的Python对象都拥有三个特性:身份、类型、值

身份:

每一个对象都有一个唯一的身份来标志自己,任何对象的身份可以使用内建函数id()来得到。这个值可以被认为是该对象的内存地址

类型:

对象的类型决定了该对象可以保存什么类型的值,可以进行怎样的操作,以及遵循什么样的规则,可以使用内建函数type()查看Python对象的类型:

代码如下:


>>> type([1,2])

>>> type(24)

>>> type({1,2,3})

>>> type('a string')

type()返回的是对象而不是简单的字符串。

值:对象表示的数据项

以上的三个特性在对象创建的时候就被赋值,除此之外,其他两个特性都是只读的

标准类型/基础数据类型:

数字、整型、布尔型、长整形、浮点型、复数型、字符串、列表、元组、字典

其他内建类型:

类型、Null对象(None)、文件、集合/固定集合、函数/方法、模块、类

None,Python的Null对象

Python有一个特殊的类型,称为Null对象或者NoneType,只有一个值:None,它不支持任何运算也没有任何内建的方法,类似于C语言的void,None类型的值

和C中的Null值非常相似

None没有什么有用的属性,它的布尔值总是False

布尔值

所有标准对象均可以用于布尔测试,同类型的对象之间可以比较大小。每一个对象天生具有布尔True或false值

空对象、值为0的任意数字或Null对象None的布尔值都是False

下列对象的布尔值都是False:

None
False(布尔类型)
所有的值为0的数
0(整型)
(浮点型)
0L(长整型)
0.0+0.0j(复数)
“”(空字符串)
[](空列表)
()(空元组)
{}(空字典)
 值不是上面列出来的任何值的对象的布尔值都是True

标准类型运算符:

比较运算符用来判断同类型对象是否相等,所有的内建类型均支持比较运算,比较运算返回布尔值True或False

代码如下:


>>> 2==2
True
>>> 2.34 True
>>> 'abc'=='xyz'
False
>>> 'xyz'>'abc'
True
>>> 'xyz' False
>>> [3,'abc']==['abc',3]
False
>>> [3,'abc']==[3,'abc']
True

多个比较操作可以在同一行上进行,求值顺序为从左到右.例如:

代码如下:


>>> 3 True
>>> 4>3==3 #等价于(4>3)and(3==3)
True
>>> 4 False

标准类型值比较运算符:

对象身份比较

每个对象都天生具有一个计数器,记录它自己的引用次数。这个数目表示有多少个变量指向该对象

Python提供了is和is not运算符来测试两个变量是否指向同一个对象

a is b  等价于  id(a)==id(b)

代码如下:


>>> foo2=foo1
>>> foo1 is foo2
True
>>> foo1 is not foo2
False
>>> id(foo1)==id(foo2)
True
>>>

布尔类型

布尔逻辑运算符and,or,not都是Python关键字,这些运算符的优先级按从高到低的顺序如下:

标准类型布尔运算符:

代码如下:


>>> x,y=3.1415926,-1024
>>> x True
>>> not(x False
>>> (x2.71828)
True
>>> (x2.71828)
False
>>> not(x is y)
True

标准类型内建函数

Python提供了一些内建函数用于这些基本对象类型:

cmp(), repr(), str(), type()和等同于repr()函数的单反引号('')运算符

type()接受一个对象作为参数,并返回它的类型。它的返回值是一个类型对象。

代码如下:


>>> type(4)

>>> type('hello!')

>>> type(type(4))

cmp()用于比较两个对象obj1和obj2,如果obj1obj2返回1,如果obj1==obj2返回0,行为类似于C中的strcmp()函数,比较是在对象之间进行的

代码如下:


>>> a,b=-4,12
>>> cmp(a,b)
-1
>>> cmp(b,a)
1
>>> b=-4
>>> cmp(a,b)
0
>>> a,b='abc','xyz'
>>> cmp(a,b)
-1
>>> cmp(b,a)
1
>>> b='abc'
>>> cmp(a,b)
0

str()和repr()(及''运算符)

内建函数str()和repr()或反引号运算符('')可以方便的以字符串的方式获得对象的内容、类型、数值属性等信息。

str()函数得到的字符串可读性好,而repr()函数得到的字符串通常可以用来重新获得该对象

代码如下:


>>> str(4.53-2j)
'(4.53-2j)'
>>> str(1)
'1'
>>> str(2e10)
'20000000000.0'

>>> str([0,5,9,9])
'[0, 5, 9, 9]'
>>> repr([0,5,9,9])
'[0, 5, 9, 9]'
>>> '[0,5,9,9]'
'[0,5,9,9]'

str()和repr()和''运算在特性和功能方面都非常的相似,repr()和''做的事情完全一样,返回的是一个对象的官方字符串表示,可以通过求值运算(使用eval()内建函数)重新得到该对象,但是str()函数则有所不同,它可以生成一个对象的可读性好的字符串表示,返回结果无法用于eval()求值,但是很适合用于print语句输出。

标准类型运算符和内建函数

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the reason why PS keeps showing loading? What is the reason why PS keeps showing loading? Apr 06, 2025 pm 06:39 PM

PS "Loading" problems are caused by resource access or processing problems: hard disk reading speed is slow or bad: Use CrystalDiskInfo to check the hard disk health and replace the problematic hard disk. Insufficient memory: Upgrade memory to meet PS's needs for high-resolution images and complex layer processing. Graphics card drivers are outdated or corrupted: Update the drivers to optimize communication between the PS and the graphics card. File paths are too long or file names have special characters: use short paths and avoid special characters. PS's own problem: Reinstall or repair the PS installer.

How to solve the problem of loading when PS is started? How to solve the problem of loading when PS is started? Apr 06, 2025 pm 06:36 PM

A PS stuck on "Loading" when booting can be caused by various reasons: Disable corrupt or conflicting plugins. Delete or rename a corrupted configuration file. Close unnecessary programs or upgrade memory to avoid insufficient memory. Upgrade to a solid-state drive to speed up hard drive reading. Reinstalling PS to repair corrupt system files or installation package issues. View error information during the startup process of error log analysis.

How to solve the problem of loading when the PS opens the file? How to solve the problem of loading when the PS opens the file? Apr 06, 2025 pm 06:33 PM

"Loading" stuttering occurs when opening a file on PS. The reasons may include: too large or corrupted file, insufficient memory, slow hard disk speed, graphics card driver problems, PS version or plug-in conflicts. The solutions are: check file size and integrity, increase memory, upgrade hard disk, update graphics card driver, uninstall or disable suspicious plug-ins, and reinstall PS. This problem can be effectively solved by gradually checking and making good use of PS performance settings and developing good file management habits.

How to use mysql after installation How to use mysql after installation Apr 08, 2025 am 11:48 AM

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

How does PS feathering control the softness of the transition? How does PS feathering control the softness of the transition? Apr 06, 2025 pm 07:33 PM

The key to feather control is to understand its gradual nature. PS itself does not provide the option to directly control the gradient curve, but you can flexibly adjust the radius and gradient softness by multiple feathering, matching masks, and fine selections to achieve a natural transition effect.

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

How to optimize database performance after mysql installation How to optimize database performance after mysql installation Apr 08, 2025 am 11:36 AM

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

What should I do if the PS card is in the loading interface? What should I do if the PS card is in the loading interface? Apr 06, 2025 pm 06:54 PM

The loading interface of PS card may be caused by the software itself (file corruption or plug-in conflict), system environment (due driver or system files corruption), or hardware (hard disk corruption or memory stick failure). First check whether the computer resources are sufficient, close the background program and release memory and CPU resources. Fix PS installation or check for compatibility issues for plug-ins. Update or fallback to the PS version. Check the graphics card driver and update it, and run the system file check. If you troubleshoot the above problems, you can try hard disk detection and memory testing.

See all articles