| title |
|---|
Python / Data Type |
Python / Data Type
-
object.eq(self, other) - 3. Data model — Python 3.7.1 documentation
__lt__()、__le__()、__eq__()、__ne__()、__gt__跟__ge__()統稱為 "rich comparison methods",很直覺分別對應<、<=、==、!=、>及>=;例如x != y背後會呼叫x.__ne__(y)。- A rich comparison method may return the singleton
NotImplementedif it does not implement the operation for a given PAIR OF ARGUMENTS (例如__eq__(self, other)就是比較self與other). By convention,FalseandTrueare returned for a SUCCESSFUL COMPARISON. 雖然可以回傳其他值,在 boolean context 時會透過bool()分出 true/false。例如if a > b:可以想成if bool(a.__gt__(b)):。 - By default,
__ne__()delegates to__eq__()and inverts the result unless it isNotImplemented. 也就是只要實作__eq__()即可,不過 Python 2 還是得實作__ne__()。 - See the paragraph on
__hash__()for some important notes on creating hashable objects which support custom comparison operations and are usable as dictionary keys. => 自訂 hashable 的 comparison 時,要注意 - There are no swapped-argument versions of these methods ... ??
-
object.eq(self, other) - 3. Data model — Python 2.7.15 documentation #ril
- There are no implied relationships among the comparison operators. The truth of
x==ydoes not imply thatx!=yis false. Accordingly, when defining__eq__(), one should also define__ne__()so that the operators will behave as expected.
- There are no implied relationships among the comparison operators. The truth of
Python 中的 None 等同於其他語言的 null,不過它是個 object,型態是 types.NoneType:
import types
def test_none_singleton():
assert isinstance(None, types.NoneType)
按照 Programming Recommendations - PEP 8 -- Style Guide for Python Code | Python.org 的說法:
Comparisons to singletons like
Noneshould always be done withisoris not, never the equality operators.
由於 None 是 singleton,比對時要用 is/is not (identity),如果用 ==/!= (equality) 可能會受到 operator overloading 的影響:
class Negator(object):
def __eq__(self, other):
return not other # doesn't make sense
def __ne__(self, other): # requried for py2
return not self.__eq__(other)
def test_none_identity__use_is():
none = None
thing = Negator()
assert none is None # singleton
assert thing is not None
def test_none_equality__donot_use_equality():
none = None
thing = Negator()
assert none == None
assert not (none != None)
# weird? the result dependes on thing.__eq__()
assert thing == None
assert not (thing != None)
參考資料:
- None - 4. Built-in Constants — Python 2.7.14 documentation
types.NoneType唯一的值就是None,表示 "absence of a value"。 - Python's null equivalent: None | Python Central (2013-06-28) 由於
null圈內人才懂 (esoteric),所以 Python 用None來表示。None是個 object (一個 class),type(None)會得到<type 'NoneType'>,在檢查是不是None時,表面上看來is跟==是一樣的,但若 class 有覆寫 comparison operator (__eq__()),那==的結果就會受到影響,所以還是用is/is not來檢查None。 - Python None comparison: should I use "is" or ==? - Stack Overflow user4815162342: 引用了 PEP 8 的說法,用
is比較 faster & more predictable,因為==會受到兩側運算子的影響。 - Programming Recommendations - PEP 8 -- Style Guide for Python Code | Python.org 直接寫明了 "Comparisons to singletons like None should always be done with is or is not, never the equality operators.",就是用
is None或is not None。 - Comparisons - 5. Built-in Types — Python 2.7.14 documentation
is/is not用在 object identity,而==/!=用在 equality。 - Comparing things to None the wrong way — Python Anti-Patterns documentation PEP 8 只是個 guideline? "It can be ignored if needed"? 但可以增加 readability。
- Python: "is None" vs "==None" 作者覺得常看到
== None也沒什麼問題,事實上也很少人自訂 comparison operators ... 這倒是?
- hashable - Glossary — Python 3.7.1rc1 documentation
- 明確定義了 An object is hashable if it has a hash value which NEVER CHANGES DURING ITS LIFETIME (it needs a
__hash__()method), and can be compared to other objects (it needs an__eq__()or__cmp__()method). Hashable objects which compare equal must have the same hash value. 也就是 hash value 相同不一定 equal,但 equal 的一定有相同的 hash value。 - Hashable 表示可以做為 dictionary key 或是 set member,因為這些資料結構內部會用到 hash value。
- All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or dictionaries) are. 明確指出所有的 immutable built-in object 都是 hashable,但 mutable container 都不是。
- 所有 user-defined class 雖然都是 hashable,但它的 hash value 都是來是
id(),所以不同 instance 一定是 unequal。
- 明確定義了 An object is hashable if it has a hash value which NEVER CHANGES DURING ITS LIFETIME (it needs a
- hash(object) - Built-in Functions — Python 3.7.1rc1 documentation
- 取得 object 的 hash value (integer);實驗發現,非 hashable 會丟出
TypeError,例如hash({})會丟出TypeError: unhashable type: 'dict'。 - Numeric values that compare equal have the same hash value (even if they are of different types, as is the case for 1 and 1.0). 這還滿有趣的,大概是 numeric type 的
__hash__()有特別考量到數值本身。 - For objects with custom
__hash__()methods, note thathash()TRUNCATES the return value based on the bit width of the host machine. 為什麼要這麼做? 不過好像也沒差,hash 本來就不是唯一值。
- 取得 object 的 hash value (integer);實驗發現,非 hashable 會丟出
- object.hash(self) - 3. Data model — Python 3.7.1rc1 documentation #ril
- Called by built-in function
hash()and for operations on members of hashed collections includingset,frozenset, anddict. 看起來 hashed collections 內部並不是透過hash()取得 hash value,而是直接調用__hash__()? - 實作上的要求很簡單,只有 "objects which compare equal have the same hash value",提到 mix together the hash values of the components of the object that also play a part in comparison of objects by PACKING THEM INTO A TUPLE AND HASHING THE TUPLE,這招還滿直觀的,例如
return hash((self.name, self.nick, self.color));因為透過 tuple 拿 hash value 的關係,也已經是被 truncate 後的版本了。 - 接下來有一大串實作
__hash__()要注意的地方 XD #ril
- Called by built-in function
- super(type[, object-or-type]) - 2. Built-in Functions — Python 2.7.15 documentation
super(type[, object-or-type])提到 If the second argument is omitted, the super object returned is unbound. 搭配 If the second argument is a type,issubclass(type2, type)must be true (this is useful for classmethods) 的說法,似乎跟 class 產生關聯就不算 unbound?
- super(type[, object-or-type]) - 2. Built-in Functions — Python 2.7.15 documentation #ril
- Return a PROXY OBJECT that delegates method calls to a PARENT or SIBLING class of
type. This is useful for accessing inherited methods that have been OVERRIDDEN in a class. 看起來就是呼叫 super class method 的方法,但怎麼會跟 sibling class 有關? - The SEARCH ORDER is same as that used by
getattr()except that the TYPE ITSELF IS SKIPPED. 這裡指的是找尋 "parent or sibling class" 的過程,跟getattr()一樣,會拿type.__mro__(tuple of classes) 依序檢查isinstance(obj, type)(若object-or-type是個 object) 或issubclass(type2, type)(檢查type2是否為type的 subclass?) ==> 所以super(ChildClass, self)的用法很合理 (也很多餘,或許 Python 3 的super()會自帶這些預設值?),但super(ParentClass, ChildClass)沒道理啊!?
- Return a PROXY OBJECT that delegates method calls to a PARENT or SIBLING class of
- super([type[, object-or-type]]) - 2. Built-in Functions — Python 3.7.0 documentation #ril
super([type[, object-or-type]])跟 Python 2 的super(type[, object-or-type])有些微不同,在 Python 3 第一個參數type也可以省略了。
- Python’s super() considered super! | Deep Thoughts by Raymond Hettinger (2011-05-26) #ril
- Working with the Python Super Function (2017-02-22) #ril