Re: [Tutor] Better construct? (was no subject)

2010-06-12 Thread bob gailer

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Better construct? (was no subject)

2010-06-12 Thread Alan Gauld


bob gailer bgai...@gmail.com wrote

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())


I suspect Bob meant to leave off the (). You would normally just
use the function name. (Unless these are factory functions
returning another function but I don't think that was the intent! :-)

funcs = (doSomething, doSomethingElse)


for func in funcs:
  getid()
  func()


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Better construct? (was no subject)

2010-06-12 Thread bob gailer

On 6/12/2010 3:59 PM, Alan Gauld wrote:


bob gailer bgai...@gmail.com wrote

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())


I suspect Bob meant to leave off the (). 


Yep. Thank you for the correction.

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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Better construct? (was no subject)

2010-06-12 Thread Luke Paireepinart

 ## 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

This seems like really strange behavior.  If the response is wrong,
you will then have another instance of getid, then if they get the
response correct, you will execute your code twice!  Was that really
your intent?
I think what you really want is:
def getid():
valid = False
while not valid:
response = raw_input('prompt')
valid = response.strip().lower() in [, y, yes]

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

Am I misunderstanding? They appear to have different behaviors.
 x = 
 y = [, y, Y, yes]
 x not in y
False
 x.lower().startswith('yes')
False
 x = yes
 x not in y
False
 x.lower().startswith('yes')
True
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor