ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] using cmd
by Kent Johnson other posts by this author
Mar 18 2006 6:00AM messages near this date
[Tutor] using cmd | Re: [Tutor] using cmd
Christopher Spears wrote:
>  I just completed an assignment out of Learning Python
>  in which I used the Cmd class from the cmd module to
>  create a little shell:
>  My main question concerns the naming of functions such
>  as:
>  
>  def do_ls(self, line):
>          if line == '': dirs = [os.curdir]
>          else: dirs = line.split()
>          for dirname in dirs:
>              print 'Listing of %s:' % dirname
>              print '\n'.join(os.listdir(dirname))
>  
>  Originally, I called the function 'ls', but when I did
>  that, the function didn't work when I typed 'ls' at
>  the prompt.  When I looked in the back of the book, I
>  saw the name the authors gave their function, which
>  was 'do_ls'.  When I changed the function's name to
>  do_ls, the function worked when I typed 'ls' at the
>  prompt.  Does anyone know why this happened?

Cmd uses introspection (using the built-in function getattr()) to find 
the handler methods. For a command named 'ls' it looks for a method 
'do_ls' to handle the command. You can also provide 'help_ls' to respond 
to the user command 'help ls'. This naming convention is built-in to Cmd 
so you have to use it for the module to work correctly.

Kent

_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Christopher Spears
Kent Johnson
Alan Gauld

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved