What's the proper style for a library string function?

Ian Kelly ian.g.kelly at gmail.com
Sat Jul 19 20:56:55 CEST 2014


On Sat, Jul 19, 2014 at 12:24 PM, Mark Lawrence <breamoreboy at yahoo.co.uk> wrote:
> Besides that I wouldn't write the function on one line, the first.  Once you
> return your data you can do what you want with it.  The second you can only
> write by default to stdout.  The third is really horrible in my book, YMMV.

I agree. Optional flags that alter the behavior of functions are
considered unpythonic; usually it's better to let the alternative
behavior have its own function, particularly if they can share
implementation.

With that in mind, I would suggest to the OP that you might want to
have *two* functions:

def format_completed_time(start, end):
    return "Time completed: " + str(end - start)

def print_completed_time(start, end):
    print(get_completed_time(start, end))

Also notice that I changed the function naming style from mixedCase to
lower_case_with_underscores. This is the style recommended for Python
by PEP 8, which you should read if you haven't already.
http://legacy.python.org/dev/peps/pep-0008/#naming-conventions

I also changed the verb from "get" to "format".  "get" suggests to me
that it will retrieve the completed time as a processable value, not
as a part of a formatted string.



More information about the Python-list mailing list