Rock It 《ML》JupyterLab 【丁】Code《二》

既然 Python 語言參考文件說︰

所有物件都有一個身份、類型和賦值。

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. The ‘is’ operator compares the identity of two objects; the id() function returns an integer representing its identity.

理應仔細推敲含意,嘗試用於各種物件,確立清楚明白陳述也。

舉例而言︰

None

This type has a single value. There is a single object with this value. This object is accessed through the built-in name None. It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don’t explicitly return anything. Its truth value is false.

類似 C 的『void』,代表『虛空』或『沒有』。

是否符合那三個條件呢?

進而釐清『意指』,最好與『空字串』、『整數零』、『空串列』 、『空字典』等等作『對比』︰

真實了解深入該物件也!

如斯者有著追根究底的精神,故不會隨便放過一字一句,往往認識深刻、理解全面呦◎

比方講,絕不會忽略下面這個

『不可改』 unchangeable

『不可變』 immutable

『可變』 mutable

區分之重要字句︰

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). Like its identity, an object’s type is also unchangeable. [1]

The value of some objects can change. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. (The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle.) An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable.

Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable.

【『不可變』通常 id() 不變】

【『不可變』可以 id() 有變】

【『可變』一定 id() 不同】

 

心中自然知道物件『拷貝』 copy 因何而生矣☆

8.10. copyShallow and deep copy operations

Source code: Lib/copy.py


Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. This module provides generic shallow and deep copy operations (explained below).

Interface summary:

copy.copy(x)
Return a shallow copy of x.
copy.deepcopy(x)
Return a deep copy of x.
exception copy.error
Raised for module specific errors.

The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):

  • A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
  • A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.

Two problems often exist with deep copy operations that don’t exist with shallow copy operations:

  • Recursive objects (compound objects that, directly or indirectly, contain a reference to themselves) may cause a recursive loop.
  • Because deep copy copies everything it may copy too much, such as data which is intended to be shared between copies.

The deepcopy() function avoids these problems by:

  • keeping a “memo” dictionary of objects already copied during the current copying pass; and
  • letting user-defined classes override the copying operation or the set of components copied.

This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the pickle module.

Shallow copies of dictionaries can be made using dict.copy(), and of lists by assigning a slice of the entire list, for example, copied_list = original_list[:].

Classes can use the same interfaces to control copying that they use to control pickling. See the description of module pickle for information on these methods. In fact, the copy module uses the registered pickle functions from the copyreg module.

In order for a class to define its own copy implementation, it can define special methods __copy__() and__deepcopy__(). The former is called to implement the shallow copy operation; no additional arguments are passed. The latter is called to implement the deep copy operation; it is passed one argument, the memo dictionary. If the__deepcopy__() implementation needs to make a deep copy of a component, it should call the deepcopy() function with the component as first argument and the memo dictionary as second argument.

See also

Module pickle
Discussion of the special methods used to support object state retrieval and restoration.

或樂知有一本 Python 2.7《 The Right Way 》指南吧︰

Fundamental Data Types

None

None
Object that denotes the lack of value.

Numbers

bool
True and False Boolean values. Evaluate to 1 and 0 respectively.
int
Integer numbers.
long
Long integer numbers.
float
Floating point numbers.
complex
Complex numbers.

Sequences

str
Strings of characters. Immutable.
`unicode`_
Unicode strings of characters. Immutable.
list
Indexed list of objects. Mutable.
tuple
Indexed list of objects. Immutable.

Mappings

dict
Hash table for storing unordered key-value pairs. Mutable.

Sets

set
Unordered list of unique objects. Mutable.

Files

file
File objects.