python核心编程第4章课后题答案(第二版75页)
2015-09-15 20:14
776 查看
4-1Python objects
All Python objects have three attributes:type,ID,and value.
All are readonly with a possible expection of the value(which can be changed only if the object is mutable).
4-5str()and repr()
repr() is a built-in function while str() was a built-in function that changed to a factory function inPython2.2.They will both returns a string representation of an object;however,str()returns a printable string representation while repr() returns an evaluatable string representation of an object,meaning that it is astring that represents a Python object that would be created if passed to eval().
4-6Object equality
type(a) == type(b) whether the value of type(a) is the same as the value of type(b)... == is a value compare
type(a) is type(b) whether the type objects returned by type(a) and type(b) are the same object
Since there exists only one (type) object for each built-in type, there is no need to check their values; hence, only the latter form should be used.
isinstance(object, class-or-type-or-tuple) -> bool
Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).
All Python objects have three attributes:type,ID,and value.
All are readonly with a possible expection of the value(which can be changed only if the object is mutable).
4-5str()and repr()
repr() is a built-in function while str() was a built-in function that changed to a factory function inPython2.2.They will both returns a string representation of an object;however,str()returns a printable string representation while repr() returns an evaluatable string representation of an object,meaning that it is astring that represents a Python object that would be created if passed to eval().
4-6Object equality
type(a) == type(b) whether the value of type(a) is the same as the value of type(b)... == is a value compare
type(a) is type(b) whether the type objects returned by type(a) and type(b) are the same object
Since there exists only one (type) object for each built-in type, there is no need to check their values; hence, only the latter form should be used.
isinstance(object, class-or-type-or-tuple) -> bool
Return whether an object is an instance of a class or of a subclass thereof.
With a type as second argument, return whether that is the object's type.
The form using a tuple, isinstance(x, (A, B, ...)), is a shortcut for
isinstance(x, A) or isinstance(x, B) or ... (etc.).
相关文章推荐
- python核心编程第3章课后题答案(第二版55页)
- python核心编程第2章课后题答案(第二版36页)
- python运算优先级
- python部分运算符理解
- python基本概念——标识符
- python基本概念——字符串
- python使用基础(win10)
- python笔记之编程风格大比拼
- Python简明教程---学习笔记
- python笔记之中缀语法和管道实现
- python笔记之bisect模块
- python笔记之常用模块用法分析
- python笔记之itertools模块
- python笔记之提取网页中的超链接
- python思想一
- python笔记之ZipFile模块
- python笔记之调用系统命令
- python笔记之hashlib模块
- python笔记之subprocess模块
- python笔记之Cmd模块