[Tutor] The better Python approach
Robert Berman
bermanrl at cfl.rr.com
Wed Jan 21 20:02:01 CET 2009
Alan,
Thank you for the clarification. Using that as my guide, I revamped my
solution to this small challenge and attempted to make the script as
concise as possible. The challenge is at the Challenge-You web page,
http://www.challenge-you.com/challenge?id=61
I am relatively certain I could have made it more concise with some
more detailed examination. If there are some obvious glaring
deficiencies, please, anyone feel free to comment.
Thanks,
Robert Berman
#!/usr/bin/env python
#findsum.py
import string
def openinput(thepath = '/home/bermanrl/usbdirbig/Challenges/sum.txt'):
''' Opens text file '''
try:
infile = open(thepath, 'r')
except:
print 'Open file failure.'
return infile
def runprocess():
'''Reads file and passes string to parsing function '''
bigtotal = 0
myfile = openinput()
for line in myfile:
jlist = line.split()
for x in jlist:
bigtotal += int(x)
print bigtotal
return
if __name__ == '__main__':
runprocess()
Alan Gauld wrote:
>
> "Robert Berman" <bermanrl at cfl.rr.com> wrote
>
>> Perhaps i should not have said the most Python correct.
>> It looks as if it may well be the approach using the least
>> amount of work the interpreter must complete.
>
> That's generally true. Python can always do things the long way but its
> generally more efficient both in programmer time and performance
> speed to use the built-in functions/methods as much as possible.
> Most of them are written in C after all!
>
> Alan G
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list