[Tutor] Better construct? (was no subject)

bob gailer bgailer at gmail.com
Sat Jun 12 17:05:52 CEST 2010


On 6/11/2010 5:12 PM, Advertising Department wrote:

Please supply a meaningful subject.

> #!/Library/Frameworks/Python.framework/Versions/Current/bin/pythonw
> """still thinking in imperative"
> """
>
> ## obviously this is a bad construct.


Why is it "obvious"?

What is "this"? Are you referring to the function, to the mainline 
program, or what?

> ## Can someone suggest a pythonesque way of doing this?
>
>
> def getid():
>     response  = raw_input('prompt')
>     if response not in [ "", "y", "Y", "yes"] :
>         getid()    # ouch
>     print "continue working"
>     # do more stuff
>     # do more stuff
>
>
> getid()
> dosomething()
> getid()
> dosomethingelse()
>
>
> ## obviously this is a bad construct.
> ## Can someone give me a pythonesque way of doing this?

One improvement - response.lower().startswith("yes") will cover all your 
cases.

You can collect functions in a sequence, then iterate over it. This 
allows for easy expansion (when you decide to add another function).

funcs = (dosomething(), dosomethingelse())
for func in funcs:
   getid()
   func()

-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list