[Tutor] AttributeError: instance has no __call__ method

Juan Shen orion_val at 163.com
Thu Dec 16 12:16:18 CET 2004


Marc Gartler wrote:

> Hi everybody,
>
> Prior to this chunk of code 'glass' has been chosen from a list of 
> colors via user input, and I now want to have that choice connect to 
> one of several possible classes:
>
> def glass_type(glasstype):
>     if glasstype == 'Red':
>         myglass = RedGlassCost()
>     elif glasstype == 'Blue':
>         myglass = BlueGlassCost()
>     elif glasstype == 'Yellow':
>         myglass = YellowGlassCost()
>     return myglass
>
> glasschoice = glass_type(glass)
> myglass = glasschoice()
>
>
> I've tried various approaches and keep getting different errors.  But 
> this one seems closest to my goal, as I know it is at least passing 
> 'glass' into the function:
>
> AttributeError: RedGlassCost instance has no __call__ method
>
> What is this trying to tell me?  Or is that irrelevant as I would be 
> better off trying some other approach altogether?
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
I have guessed your meaning by that code.  Try the following:

glasstype={\
'Red':RedGlassCost,\
'Blue':BlueGlassCost,\
'Yellow':YellowGlassCost}
# RedGlassCost ... are types.FunctionType
myglass=glasstype[glass]()



More information about the Tutor mailing list