Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74829

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

References <53CAAD27.9010207@cdreimer.com> <lqed60$si$1@ger.gmane.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2014-07-19 12:56 -0600
Subject Re: What's the proper style for a library string function?
Newsgroups comp.lang.python
Message-ID <mailman.12057.1405796265.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Jul 19, 2014 at 12:24 PM, Mark Lawrence <breamoreboy@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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: What's the proper style for a library string function? Ian Kelly <ian.g.kelly@gmail.com> - 2014-07-19 12:56 -0600

csiph-web