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


Groups > comp.lang.python > #74838 > unrolled thread

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

Started byWojciech Giel <wojtekgiel@gmail.com>
First post2014-07-19 19:40 +0100
Last post2014-07-19 19:40 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: What's the proper style for a library string function? Wojciech Giel <wojtekgiel@gmail.com> - 2014-07-19 19:40 +0100

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

FromWojciech Giel <wojtekgiel@gmail.com>
Date2014-07-19 19:40 +0100
SubjectRe: What's the proper style for a library string function?
Message-ID<mailman.12064.1405807535.18130.python-list@python.org>
On 19/07/14 18:38, C.D. Reimer wrote:
> Greetings,
>
> I typically write a Python 2.7 string function in my library like this:
>
>     def getCompletedTime(start, end): return "Time completed:", 
> str(end - start)
>
> And called it like this:
>
>     print getCompletedTime(start, end)
>
> Since every Python script I write is executed from the command line, I 
> rewrote the string function like this:
>
>     def getCompletedTime(start, end): print "Time completed:", str(end 
> - start)
>
> And call it like this:
>
>     getCompletedTime(start, end)
>
> The first version is what I'm familiar with having reluctantly learned 
> Java at community college, which couldn't afford a Microsoft site 
> license for Visual C++ and taught every class in Java. (The Linux 
> instructor rebelled against this policy by teaching basic C/C++ and 
> shell scripting in his classes.) I recently read an article that 
> Python is replacing Java as a teaching language.
>
> The second version is more straight forward but seems less readable 
> (i.e., "print getCompletedTime(start, end)" vs. 
> "getCompletedTime(start, end)") from the calling script.
>
> Alternatively, I thought about rewriting the string function to accept 
> an extra parameter to do either and default to the print statement.
>
>     def getCompletedTime(start, end, type = 'p'):
>         string = "Time completed: " + str(end - start)
>         if type == 'p':
>             print string
>         else:
>             return string
>
> I'm curious as to what the proper Python style would be for this.
You might look into PEP8 "Style Guide for Python Code" it will give you 
recommendation how to write a code. among other gives most sensible answer:
  "Consistency within a project is more important. Consistency within 
one module or function is most important...... When in doubt, use your 
best judgment. Look at other examples and decide what looks best."

http://legacy.python.org/dev/peps/pep-0008/#function-names

for me if you will be reusing this function I would just use:
  def getCompletedTime(start, end): return "Time completed:", str(end - 
start)

if you need to print just add print, if you want result from this 
function somewhere else you got it. adding additional arguments 
necessary adds complexity. do you really need it. Keep it simple stupid 
expression can be applied everywhere.
cheers
Wojciech

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web