diff --git a/advanced_topics/04_operator_overloading.ipynb b/advanced_topics/04_operator_overloading.ipynb index 567fd7e6ade801b930979254bc95954d723f34d9..c860fb3391cc115296667afe418fa559ef28d323 100644 --- a/advanced_topics/04_operator_overloading.ipynb +++ b/advanced_topics/04_operator_overloading.ipynb @@ -14,13 +14,13 @@ "Operator overloading, in an object-oriented programming language, is the\n", "process of customising the behaviour of _operators_ (e.g. `+`, `*`, `/` and\n", "`-`) on user-defined types. This practical aims to show you that operator\n", - "overloading is __very__ easy to do in Python.\n", + "overloading is **very** easy to do in Python.\n", "\n", "\n", "This practical gives a brief overview of the operators which you may be most\n", "interested in implementing. However, there are many operators (and other\n", "special methods) which you can support in your own classes - the [official\n", - "documentation](https://docs.python.org/3.5/reference/datamodel.html#basic-customization)\n", + "documentation](https://docs.python.org/3/reference/datamodel.html#basic-customization)\n", "is the best reference if you are interested in learning more.\n", "\n", "\n", @@ -243,7 +243,7 @@ "operands. For example, in the expression `a + b`, if `a.__add__` is not\n", "implemented, but but `b.__radd__` is implemented, then the latter will be\n", "called. Take a look at the [official\n", - "documentation](https://docs.python.org/3.5/reference/datamodel.html#emulating-numeric-types)\n", + "documentation](https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types)\n", "for further details, including a full list of the arithmetic and logical\n", "operators that your classes can support.\n", "\n", @@ -346,9 +346,9 @@ "metadata": {}, "source": [ "The\n", - "[`@functools.total_ordering`](https://docs.python.org/3.5/library/functools.html#functools.total_ordering)\n", + "[`@functools.total_ordering`](https://docs.python.org/3/library/functools.html#functools.total_ordering)\n", "is a convenience\n", - "[decorator](https://docs.python.org/3.5/glossary.html#term-decorator) which,\n", + "[decorator](https://docs.python.org/3/glossary.html#term-decorator) which,\n", "given a class that implements equality and a single comparison function\n", "(`__lt__` in the above code), will \"fill in\" the remainder of the comparison\n", "operators. If you need very specific or complicated behaviour, then you can\n", @@ -365,7 +365,7 @@ "\n", "\n", "Refer to the [official\n", - "documentation](https://docs.python.org/3.5/reference/datamodel.html#object.__lt__)\n", + "documentation](https://docs.python.org/3/reference/datamodel.html#object.__lt__)\n", "for all of the details on supporting comparison operators.\n", "\n", "\n", @@ -387,13 +387,13 @@ "with the `[]` operator. All that is needed to support them are to implement\n", "three special methods in your class, regardless of whether your class will be\n", "indexed by sequential integers (like a `list`) or by\n", - "[hashable](https://docs.python.org/3.5/glossary.html#term-hashable) values\n", + "[hashable](https://docs.python.org/3/glossary.html#term-hashable) values\n", "(like a `dict`):\n", "\n", "\n", - "- __Retrieval__ is performed by the `__getitem__` method\n", - "- __Assignment__ is performed by the `__setitem__` method\n", - "- __Deletion__ is performed by the `__delitem__` method\n", + "- **Retrieval** is performed by the `__getitem__` method\n", + "- **Assignment** is performed by the `__setitem__` method\n", + "- **Deletion** is performed by the `__delitem__` method\n", "\n", "\n", "Note that, if you implement these methods in your own class, there is no\n", @@ -496,7 +496,7 @@ "metadata": {}, "source": [ "If you wish to support the Python `start:stop:step` [slice\n", - "notation](https://docs.python.org/3.5/library/functions.html#slice), you\n", + "notation](https://docs.python.org/3/library/functions.html#slice), you\n", "simply need to write your `__getitem__` and `__setitem__` methods so that they\n", "can detect `slice` objects:" ] @@ -556,7 +556,7 @@ "> different hashing algorithm), the `Sequence` and `MutableMapping` classes\n", "> are [a better choice](https://stackoverflow.com/a/7148602) - you can find\n", "> them in the\n", - "> [`collections.abc`](https://docs.python.org/3.5/library/collections.abc.html)\n", + "> [`collections.abc`](https://docs.python.org/3/library/collections.abc.html)\n", "> module.\n", "\n", "\n", @@ -630,7 +630,7 @@ "metadata": {}, "source": [ "> The `TimedFunction` class is conceptually very similar to a\n", - "> [decorator](https://docs.python.org/3.5/glossary.html#term-decorator) -\n", + "> [decorator](https://docs.python.org/3/glossary.html#term-decorator) -\n", "> decorators are covered in another practical.\n", "\n", "\n", @@ -643,7 +643,7 @@ "quite a niche feature, and it is easy to trip yourself up, so if you wish to\n", "use this in your own project, make sure that you carefully read (and\n", "understand) [the\n", - "documentation](https://docs.python.org/3.5/reference/datamodel.html#customizing-attribute-access),\n", + "documentation](https://docs.python.org/3/reference/datamodel.html#customizing-attribute-access),\n", "and test your code comprehensively!\n", "\n", "\n", diff --git a/advanced_topics/04_operator_overloading.md b/advanced_topics/04_operator_overloading.md index 3446401f9fa65815b69b2617ba76edcf85747d9c..ed386e736f32e1466d7d67fc1219ce834c49d07a 100644 --- a/advanced_topics/04_operator_overloading.md +++ b/advanced_topics/04_operator_overloading.md @@ -8,13 +8,13 @@ Operator overloading, in an object-oriented programming language, is the process of customising the behaviour of _operators_ (e.g. `+`, `*`, `/` and `-`) on user-defined types. This practical aims to show you that operator -overloading is __very__ easy to do in Python. +overloading is **very** easy to do in Python. This practical gives a brief overview of the operators which you may be most interested in implementing. However, there are many operators (and other special methods) which you can support in your own classes - the [official -documentation](https://docs.python.org/3.5/reference/datamodel.html#basic-customization) +documentation](https://docs.python.org/3/reference/datamodel.html#basic-customization) is the best reference if you are interested in learning more. @@ -173,7 +173,7 @@ rules are followed depending on the set of methods implemented on the operands. For example, in the expression `a + b`, if `a.__add__` is not implemented, but but `b.__radd__` is implemented, then the latter will be called. Take a look at the [official -documentation](https://docs.python.org/3.5/reference/datamodel.html#emulating-numeric-types) +documentation](https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types) for further details, including a full list of the arithmetic and logical operators that your classes can support. @@ -252,9 +252,9 @@ print(sorted((l3, l1, l2))) The -[`@functools.total_ordering`](https://docs.python.org/3.5/library/functools.html#functools.total_ordering) +[`@functools.total_ordering`](https://docs.python.org/3/library/functools.html#functools.total_ordering) is a convenience -[decorator](https://docs.python.org/3.5/glossary.html#term-decorator) which, +[decorator](https://docs.python.org/3/glossary.html#term-decorator) which, given a class that implements equality and a single comparison function (`__lt__` in the above code), will "fill in" the remainder of the comparison operators. If you need very specific or complicated behaviour, then you can @@ -271,7 +271,7 @@ and just one of `__lt__`, `__le__`, `__gt__` or `__ge__`. Refer to the [official -documentation](https://docs.python.org/3.5/reference/datamodel.html#object.__lt__) +documentation](https://docs.python.org/3/reference/datamodel.html#object.__lt__) for all of the details on supporting comparison operators. @@ -293,13 +293,13 @@ At its essence, there are only three types of behaviours that are possible with the `[]` operator. All that is needed to support them are to implement three special methods in your class, regardless of whether your class will be indexed by sequential integers (like a `list`) or by -[hashable](https://docs.python.org/3.5/glossary.html#term-hashable) values +[hashable](https://docs.python.org/3/glossary.html#term-hashable) values (like a `dict`): -- __Retrieval__ is performed by the `__getitem__` method -- __Assignment__ is performed by the `__setitem__` method -- __Deletion__ is performed by the `__delitem__` method +- **Retrieval** is performed by the `__getitem__` method +- **Assignment** is performed by the `__setitem__` method +- **Deletion** is performed by the `__delitem__` method Note that, if you implement these methods in your own class, there is no @@ -370,7 +370,7 @@ print(tt['12345']) If you wish to support the Python `start:stop:step` [slice -notation](https://docs.python.org/3.5/library/functions.html#slice), you +notation](https://docs.python.org/3/library/functions.html#slice), you simply need to write your `__getitem__` and `__setitem__` methods so that they can detect `slice` objects: @@ -414,7 +414,7 @@ print(tt[::2]) > different hashing algorithm), the `Sequence` and `MutableMapping` classes > are [a better choice](https://stackoverflow.com/a/7148602) - you can find > them in the -> [`collections.abc`](https://docs.python.org/3.5/library/collections.abc.html) +> [`collections.abc`](https://docs.python.org/3/library/collections.abc.html) > module. @@ -472,7 +472,7 @@ inv = tf(data) > The `TimedFunction` class is conceptually very similar to a -> [decorator](https://docs.python.org/3.5/glossary.html#term-decorator) - +> [decorator](https://docs.python.org/3/glossary.html#term-decorator) - > decorators are covered in another practical. @@ -485,7 +485,7 @@ the attributes and methods of an object. This is very powerful, but is also quite a niche feature, and it is easy to trip yourself up, so if you wish to use this in your own project, make sure that you carefully read (and understand) [the -documentation](https://docs.python.org/3.5/reference/datamodel.html#customizing-attribute-access), +documentation](https://docs.python.org/3/reference/datamodel.html#customizing-attribute-access), and test your code comprehensively!