[Tutor] Better printing?

Alan Gauld alan.gauld at btinternet.com
Mon Jan 22 18:44:36 CET 2007


"Steve Nelson" <sanelson at gmail.com> wrote 

> def getSize(fn):
>  """Get the size of a file in kb"""
>  fp = os.path.join(wp, fn)
>  if os.path.isfile(fp):
>    return os.stat(fp).st_size

One problem with all of these is that you only return 
a value if it is a file. If its not a file you return None
(the default return value). It would be easier to maintain 
if you make the return explicit.

Personally I would probably raise an exception 
(maybe a TypeError) if it's not a file. That puts the onus 
on the client to check that they pass something that 
you can get the size of - or use a try/except to catch 
the invalid entries..

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list