instance has no __call__ method

Steve Holden steve at holdenweb.com
Sat Dec 11 09:24:54 EST 2010


On 12/10/2010 5:20 AM, 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?
> 
> Thanks a lot in advance.
> 
> Regards
> Frank.Cui
> 
Try renaming your .calc() method to .__call__(). That way the method
will be called when you perform a function call on an instance.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17       http://us.pycon.org/
See Python Video!       http://python.mirocommunity.org/
Holden Web LLC                 http://www.holdenweb.com/




More information about the Python-list mailing list