https://github.com/pj/hacky_python_typeclasses
Typeclasses for Python (badly)
https://github.com/pj/hacky_python_typeclasses
Last synced: 8 months ago
JSON representation
Typeclasses for Python (badly)
- Host: GitHub
- URL: https://github.com/pj/hacky_python_typeclasses
- Owner: pj
- Created: 2015-05-27T22:54:03.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-05-27T22:54:13.000Z (about 11 years ago)
- Last Synced: 2025-02-16T19:14:49.486Z (over 1 year ago)
- Language: Python
- Size: 102 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hacky typeclasses for Python
Basically the idea is to use python decorators to fake Haskell typeclasses (badly).
To create a method for a typeclass:
```python
from typeclass import typemethod
class Show(object):
pass
@typemethod(Show)
def show(val):
# If there is no default then just raise NotImplementedError()
return str(val)
```
To create an instance:
```python
from typeclass import typeinstance
import show
class Something(object):
def __init__(self, a):
self.a = a
@typeinstance(Something, show.show)
def show(val):
return "Something: " + str(val.a)
```
Then use it by importing the show method from the typeclass and ensuring that
the instance is loaded:
```python
from show import show
from something import Something
print show(Something(42))
```