Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #74823 > unrolled thread
| Started by | "C.D. Reimer" <chris@cdreimer.com> |
|---|---|
| First post | 2014-07-19 10:38 -0700 |
| Last post | 2014-07-20 01:41 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
What's the proper style for a library string function? "C.D. Reimer" <chris@cdreimer.com> - 2014-07-19 10:38 -0700
Re: What's the proper style for a library string function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-20 01:41 +0000
| From | "C.D. Reimer" <chris@cdreimer.com> |
|---|---|
| Date | 2014-07-19 10:38 -0700 |
| Subject | What's the proper style for a library string function? |
| Message-ID | <mailman.12051.1405791581.18130.python-list@python.org> |
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.
Thank you,
Chris Reimer
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-07-20 01:41 +0000 |
| Message-ID | <53cb1e54$0$6574$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #74823 |
On Sat, 19 Jul 2014 10:38:47 -0700, 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) In general, I much prefer the first version. The general principle I follow is, as much as possible, to separate calculation from display. Advantages include: - You can easily re-use the getCompletedTime function in situations where you don't want to display the time, or pass the result on to another function for further processing before display. - It makes automated testing of the functions much easier. - It makes it easier to vary the type of display. Today you want to print to the standard output. Tomorrow you might want to output to a file, or display in a GUI window. - It makes it easier to understand the large-scale structure of your code. Seeing "print getCompletedTime(a, b)" makes it pretty clear that you are printing the result of the calculation, whereas "getCompletedTime(a, b)" does not. -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web