zoukankan      html  css  js  c++  java
  • objects & values & types

    [objects & values & types]

    1、Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory.

    2、The ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity (currently implemented as its address).

    3、An object’s type is also unchangeable. An object’s type determines the operations that the object supports (e.g., “does it have a length?”) and also defines the possible values for objects of that type. The type() function returns an object’s type (which is an object itself).

    4、An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable.

    5、Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. 

    6、CPython implementation detail: CPython currently uses a reference-counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references

    7、Some objects contain references to “external” resources such as open files or windows. It is understood that these resources are freed when the object is garbage-collected, but since garbage collection is not guaranteed to happen, such objects also provide an explicit way to release the external resource, usually a close() method. Programs are strongly recommended to explicitly close such objects. The ‘try...finally‘ statement provides a convenient way to do this.

      垃圾回收时,资源会被自动释放。但垃圾回收无法保证及时触发。所以尽量调用close() 释放释放资源。

    8、For immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation, but after c = []; d = []c and d are guaranteed to refer to two different, unique, newly created empty lists. (Note that c = d = [] assigns the same object to both c andd.)

    参考: http://docs.python.org/2.7/reference/datamodel.html#objects-values-and-types

  • 相关阅读:
    1312. Minimum Insertion Steps to Make a String Palindrome
    【堆】23. Merge k Sorted Lists
    LeetCode 406 根据身高重建队列
    LeetCode 922 按奇偶排序数组II
    LeetCode 31 下一个排列
    LeetCode 941 有效的山脉数组
    LeetCode 面试题4 二维数组中的查找
    LeetCode 463 岛屿的周长
    LeetCode 129 求根到叶子节点数字之和
    LeetCode 1207 独一无二的出现次数
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3449662.html
Copyright © 2011-2022 走看看