python核心编程第4章课后题答案(第二版75页)

来源:互联网 发布:mac java 安全设置 编辑:程序博客网 时间:2024/04/28 18:30
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.).

0 0
原创粉丝点击