Wouldn't it be nice if this worked?

Stefan Behnel stefan_ml at behnel.de
Sun Aug 10 16:14:13 EDT 2008


Suresh V. wrote:
> class A:
>     def add(self, x, y):
>         return x+y
> 
> class B:
>     pass
> 
> B.add = A.add
> 
> print B().add(1, 2) <----- gives TypeError: unbound method add() must
>                            be called with A instance as first argument

Works for me:

  Python 3.0b2 (r30b2:65080, Jul 20 2008, 20:03:32)
  [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> class A:
  ...     def add(self, x, y):
  ...         return x+y
  ...
  >>> class B:
  ...     pass
  ...
  >>> B.add = A.add
  >>>
  >>> print(B().add(1, 2))
  3

Stefan



More information about the Python-list mailing list