[Tutor] Interactive Menu Woes

Bryan Magalski bryan_magalski at yahoo.com
Thu Nov 15 13:20:43 CET 2007


Awesome, awesome, awesome.  I will check my script when I get home from work, but your explanation is top notch!  I think I understand it now.  Thank you.

I will post my corrected script and the answers when I get home from work tonight.

Many thanks!


----- Original Message ----
From: Evert Rol <evert.rol at gmail.com>
To: Bryan Magalski <bryan_magalski at yahoo.com>
Cc: tutor at python.org
Sent: Thursday, November 15, 2007 11:37:43 AM
Subject: Re: [Tutor] Interactive Menu Woes

> Thank you for your suggestion.  I did not create the original  
> script, so it will stay as is and my addition for the menu has been  
> adjusted.
>
> Now that I can make a clear distinction of what I am returning, I  
> am getting a new error that leads me that I am trying to call a  
> function that cannot be seen when instantiated:
>
> To add a name, please input he following information:
> Name: Bryan
> Number: 1234567890
> What type of number is this? (choose one):
>                      1. Home:
>                      2. Work:
>                      3. Cell:
>                      : 1
> Traceback (most recent call last):
>  File "menu_modified.py", line 95, in <module>
>    menu.addName()
>  File "menu_modified.py", line 73, in addName
>    enter(name, number, returnType)
> AttributeError: phoneentry instance has no __call__ method


You're first creating a phoneentry object, and then calling the  
actual object. That doesn't work; it's somewhat similar to:
>>> a = dict()
>>> a(key=5)
which gives a slightly different error, but obviously the correct  
form is:
>>> a = dict(key=5)
and also
>>> phoneentry(name, number, returnType)

When creating an object (instantiating the class), the __init__  
method is automatically called, and you provide the arguments of the  
__init__ method when creating the object.
In your current script, the actual entry you create (enter =  
phoneenetry()) creates an entry with name & number 'Unknown' (default  
arguments), and types=UNKNOWN; then you want to assign the actual  
values to the entry. If you *really* want to do this (don't, though),  
you'd be doing:
entry = phoneentry()
entry.name = name
entry.number = number
entry.types = returnType

See the add() method in the phonedb class, where it is done correctly.


A few other thoughts:
- what does numberType return if 'n is not in typeDict'? It should  
return UNKNOWN I guess.
- why not remove that function, and put typeDict to addName? It's  
really only one extra line, and avoids the whole extra function call  
(replacing it with something one might call a 'dictionary call')
- I noticed you use tabs, while the original part of the code uses  
spaces. Try to avoid mixing them: at some point things will go wrong  
(spaces are preferred; for a good read on that and other stuff:  
http://www.python.org/doc/essays/styleguide.html )


  Evert


      ____________________________________________________________________________________
Be a better sports nut!  Let your teams follow you 
with Yahoo Mobile. Try it now.  http://mobile.yahoo.com/sports;_ylt=At9_qDKvtAbMuh1G1SQtBI7ntAcJ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071115/412135d3/attachment.htm 


More information about the Tutor mailing list