AndreaCensi/contracts — pull request #29 by yaniv256 (2014-11-18 06:34:55+00:00)
Title: Support for calllable objects and bound functions

Hi All,

My first push request on github, and quite new to python in general . I added support for callable objects and bound functions. On 2.7:

```python
from contracts import contract, ContractNotRespected

class eggs(object):
def __init__(self,x):
self.x=x
def __call__(self,kw1,kw2):
print str(self.x) + ': kw1='+ str(kw1) + ', kw2=' + str(kw2)
def bound_func(self,kw1,kw2):
print str(self.x) + ': kw1='+ str(kw1) + ', kw2=' + str(kw2)

spam=eggs('Hello from within the instance')
fish=contract(kw1=float,kw2=int)(spam)

print "\nCalling a proper fish"
fish(6.0,5)

print "\nCalling an improper fish"
try:
fish(6,5.0)
except ContractNotRespected as detail:
print detail

dead=eggs("And now ...")
parrot=contract(kw1=float,kw2=int)(dead.bound_func)

print "\nCalling a proper parrot"
parrot(600.0,500)

print "\nCalling an improper parrot"
try:
parrot(600,500.0)
except ContractNotRespected as detail:
print detail
```

The output is:

```
Calling a proper fish
Hello from within the instance: kw1=6.0, kw2=5

Calling an improper fish
Breach for argument 'kw1' to __call__().
Expected type 'float', got 'int'.
checking: float for value: Instance of int: 6

Calling a proper parrot
And now ...: kw1=600.0, kw2=500

Calling an improper parrot
Breach for argument 'kw1' to bound_func().
Expected type 'float', got 'int'.
checking: float for value: Instance of int: 600
```