instance has no __call__ method

Dave Angel davea at ieee.org
Fri Dec 10 06:28:39 EST 2010


On 01/-10/-28163 02:59 PM, frank cui wrote:
> Hi all,
>
> I'm a novice learner of python and get caught in the following trouble and
> hope experienced users can help me solve it:)
>
> Code:
> -----------------------------------------------------------------------
> $ cat Muffle_ZeroDivision.py
> #!/usr/bin/env python
>
> class MuffledCalculator:
>      muffled = False
>      def clac(self,expr):
>          try:
>              return eval(expr)
>          except:
>              if self.muffled:
>                  print 'Division by zero is illegal'
>              else:
>                  raise
> --------------------------------------------------------------------------
>
> $ python
> Python 2.7 (r27:82500, Sep 16 2010, 18:03:06)
> [GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import Muffle_ZeroDivision
>>>> calc = Muffle_ZeroDivision.MuffledCalculator()
>>>> calc = ('10/2')
>>>> calc = Muffle_ZeroDivision.MuffledCalculator()
>>>> calc('10/2')
> Traceback (most recent call last):
>    File "<stdin>", line 1, in<module>
> AttributeError: MuffledCalculator instance has no __call__ method
>
> ----------------------------------------------------------------------------
>
> There is an AttributeError that this instance doesn't have the __call__
> method, so how to add this kind of method to my instance?
>

Your problem is that you defined a clac() method, but never call it.  So 
instead of calc('10/2') you need

calc.clac("10/2")

I'm not sure why you have such a confusing name, however.

DaveA



More information about the Python-list mailing list