Skip to content

Commit bc0f872

Browse files
authored
Merge pull request #16 from pganssle/singledispatch_docs
Singledispatch docs
2 parents 9839148 + 4320fc8 commit bc0f872

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

README.rst

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,72 @@ To create a function with variants, simply decorate the primary form with ``@var
6363
print_text.from_url(hw_url) # Hello, world! (from url)
6464
6565
66+
Differences from singledispatch
67+
-------------------------------
68+
69+
While ``variants`` and |singledispatch|_ are both intended to provide alternative implementations
70+
to a primary function, the overall aims are slightly different. ``singledispatch`` transparently
71+
dispatches to variant functions based on the *type* of the argument, whereas ``variants`` provides
72+
*explicit* alternative forms of the function. Note that in the above example, both
73+
``print_text.from_filepath`` and ``print_text.from_url`` take a string, one representing a file
74+
path and one representing a URL.
75+
76+
Additionally, the ``variants`` is compatible with ``singledispatch``, so you can have the best of
77+
both worlds; an example that uses both:
78+
79+
80+
.. code-block:: python
81+
82+
@variants.primary
83+
@singledispatch
84+
def add(x, y):
85+
return x + y
86+
87+
@add.variant('from_list')
88+
@add.register(list)
89+
def add(x, y):
90+
return x + [y]
91+
92+
Which then automatically dispatches between named variants based on type:
93+
94+
.. code-block:: python
95+
96+
>>> add(1, 2)
97+
3
98+
>>> add([1], 2)
99+
[1, 2]
100+
101+
But also exposes the explicit variant functions:
102+
103+
.. code-block:: python
104+
105+
>>> add.from_list([1], 2)
106+
[1, 2]
107+
>>> add.from_list()
108+
7 @add.register(list)
109+
8 def add(x, y):
110+
----> 9 return x + [y]
111+
112+
TypeError: unsupported operand type(s) for +: 'int' and 'list'
113+
114+
It is important to note that the ``variants`` decorators **must be the outer decorators**.
115+
116+
117+
Installation
118+
------------
119+
120+
To install variants, run this command in your terminal:
121+
122+
.. code-block:: console
123+
124+
$ pip install variants
125+
126+
66127
Requirements
67128
------------
68129

69130
This is a library for Python, with support for versions 2.7 and 3.4+.
70131

132+
.. |singledispatch| replace:: ``singledispatch``
133+
.. _singledispatch: https://docs.python.org/3/library/functools.html#functools.singledispatch
134+

docs/installation.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ You can either clone the public repository:
3232

3333
.. code-block:: console
3434
35-
$ git clone git://github.com/pganssle/variants
35+
$ git clone git://github.com/python-variants/variants
3636
3737
Or download the `tarball`_:
3838

3939
.. code-block:: console
4040
41-
$ curl -OL https://github.com/pganssle/variants/tarball/master
41+
$ curl -OL https://github.com/python-variants/variants/tarball/master
4242
4343
Once you have a copy of the source, you can install it with:
4444

@@ -47,5 +47,5 @@ Once you have a copy of the source, you can install it with:
4747
$ python setup.py install
4848
4949
50-
.. _Github repo: https://github.com/pganssle/variants
51-
.. _tarball: https://github.com/pganssle/variants/tarball/master
50+
.. _Github repo: https://github.com/python-variants/variants
51+
.. _tarball: https://github.com/python-variants/variants/tarball/master

0 commit comments

Comments
 (0)