* **Return value of repr() should be unambiguous and of str() readable.**
* **If only repr() is defined, it will be also used for str().**
* **If only repr() is defined, it will also be used for str().**
#### Str() use cases:
```python
@ -1065,7 +1065,7 @@ Duck Types
### Comparable
* **If eq() method is not overridden, it returns `'id(self) == id(other)'`, which is the same as `'self is other'`.**
* **That means all objects compare not equal by default.**
* **Only left side object has eq() method called, unless it returns 'NotImplemented', in which case the right object is consulted.**
* **Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted.**
```python
class MyComparable:
@ -1117,7 +1117,7 @@ class MySortable:
```
### Iterator
* **Next() should return next item or raise 'StopIteration'.**
* **Next() should return next item or raise StopIteration.**
* **Iter() should return 'self'.**
```python
class Counter:
@ -1226,7 +1226,7 @@ class MyCollection:
### Sequence
* **Only required methods are len() and getitem().**
* **Getitem() should return an item at index or raise 'IndexError'.**
* **Getitem() should return an item at index or raise IndexError.**
* **Iter() and contains() automatically work on any object that has getitem() defined.**
* **Reversed() automatically works on any object that has getitem() and len() defined.**
```python
@ -1248,7 +1248,7 @@ class MySequence:
### Collections.abc.Sequence
* **It's a richer interface than the basic sequence.**
* **Extending it generates iter(), contains(), reversed(), index(), and count().**
* **Unlike `'abc.Iterable'` and `'abc.Collection'`, it is not a duck type. That is why `'issubclass(MySequence, collections.abc.Sequence)'` would return 'False' even if 'MySequence' had all the methods defined.**
* **Unlike `'abc.Iterable'` and `'abc.Collection'`, it is not a duck type. That is why `'issubclass(MySequence, collections.abc.Sequence)'` would return False even if MySequence had all the methods defined.**
```python
class MyAbcSequence(collections.abc.Sequence):
@ -2096,7 +2096,7 @@ class MyMetaClass(type):
return type.__new__(cls, name, parents, attrs)
```
* **New() is a class method that gets called before init(). If it returns an instance of its class, then that instance gets passed to init() as a 'self' argument.**
* **It receives the same arguments as init(), except for the first one that specifies the desired class of returned instance (**`'MyMetaClass'`**in our case).**
* **It receives the same arguments as init(), except for the first one that specifies the desired class of returned instance (MyMetaClass in our case).**
* **New() can also be called directly, usually from a new() method of a child class (**`def __new__(cls): return super().__new__(cls)`**), in which case init() is not called.**
@ -1008,7 +1008,7 @@ Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span clas
<div><h2id="ducktypes"><ahref="#ducktypes"name="ducktypes">#</a>Duck Types</h2><p><strong>A duck type is an implicit type that prescribes a set of special methods. Any object that has those methods defined is considered a member of that duck type.</strong></p><div><h3id="comparable">Comparable</h3><ul>
<li><strong>If eq() method is not overridden, it returns <codeclass="python hljs"><spanclass="hljs-string">'id(self) == id(other)'</span></code>, which is the same as <codeclass="python hljs"><spanclass="hljs-string">'self is other'</span></code>.</strong></li>
<li><strong>That means all objects compare not equal by default.</strong></li>
<li><strong>Only left side object has eq() method called, unless it returns 'NotImplemented', in which case the right object is consulted.</strong></li>
<li><strong>Only the left side object has eq() method called, unless it returns NotImplemented, in which case the right object is consulted.</strong></li>
<li><strong>It's a richer interface than the basic sequence.</strong></li>
<li><strong>Extending it generates iter(), contains(), reversed(), index(), and count().</strong></li>
<li><strong>Unlike <codeclass="python hljs"><spanclass="hljs-string">'abc.Iterable'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'abc.Collection'</span></code>, it is not a duck type. That is why <codeclass="python hljs"><spanclass="hljs-string">'issubclass(MySequence, collections.abc.Sequence)'</span></code> would return 'False' even if 'MySequence' had all the methods defined.</strong></li>
<li><strong>Unlike <codeclass="python hljs"><spanclass="hljs-string">'abc.Iterable'</span></code> and <codeclass="python hljs"><spanclass="hljs-string">'abc.Collection'</span></code>, it is not a duck type. That is why <codeclass="python hljs"><spanclass="hljs-string">'issubclass(MySequence, collections.abc.Sequence)'</span></code> would return False even if MySequence had all the methods defined.</strong></li>
<li><strong>New() is a class method that gets called before init(). If it returns an instance of its class, then that instance gets passed to init() as a 'self' argument.</strong></li>
<li><strong>It receives the same arguments as init(), except for the first one that specifies the desired class of returned instance (</strong><codeclass="python hljs"><spanclass="hljs-string">'MyMetaClass'</span></code><strong>in our case).</strong></li>
<li><strong>It receives the same arguments as init(), except for the first one that specifies the desired class of returned instance (MyMetaClass in our case).</strong></li>
<li><strong>New() can also be called directly, usually from a new() method of a child class (</strong><codeclass="python hljs"><spanclass="hljs-function"><spanclass="hljs-keyword">def</span><spanclass="hljs-title">__new__</span><spanclass="hljs-params">(cls)</span>:</span><spanclass="hljs-keyword">return</span> super().__new__(cls)</code><strong>), in which case init() is not called.</strong></li>
</ul>
<div><h3id="metaclassattribute">Metaclass Attribute</h3><p><strong>Right before a class is created it checks if it has metaclass defined. If not, it recursively checks if any of his parents has it defined and eventually comes to type().</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-class"><spanclass="hljs-keyword">class</span><spanclass="hljs-title">MyClass</span><spanclass="hljs-params">(metaclass=MyMetaClass)</span>:</span>
xxxxxxxxxx