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


Groups > comp.lang.python > #74838

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <wojtekgiel@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'string': 0.09; 'arguments': 0.09; 'code"': 0.09; 'consistency': 0.09; 'executed': 0.09; 'parameter': 0.09; 'scripting': 0.09; 'subject:string': 0.09; 'python': 0.11; 'def': 0.12; 'cheers': 0.12; '2.7': 0.14; 'language.': 0.14; 'c/c++': 0.16; 'java.': 0.16; 'pep8': 0.16; 'readable': 0.16; 'rebelled': 0.16; 'rewriting': 0.16; 'statement.': 0.16; 'subject:library': 0.16; 'url:pep-0008': 0.16; 'url:peps': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'library': 0.18; 'module': 0.19; 'replacing': 0.19; 'examples': 0.20; 'seems': 0.21; '(the': 0.22; 'command': 0.22; 'shell': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'adds': 0.24; 'script.': 0.24; 'url:dev': 0.24; 'decide': 0.24; 'java': 0.24; 'looks': 0.24; 'script': 0.25; 'this:': 0.26; 'second': 0.26; 'somewhere': 0.26; 'skip:" 20': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'important.': 0.30; "i'm": 0.30; 'gives': 0.31; 'end,': 0.31; 'class': 0.32; 'this.': 0.32; 'linux': 0.33; 'url:python': 0.33; 'community': 0.33; 'style': 0.33; 'subject:the': 0.34; 'basic': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'version': 0.36; 'really': 0.36; 'c++': 0.36; 'curious': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'project': 0.37; 'message-id:@gmail.com': 0.38; '(i.e.,': 0.38; 'to:addr:python-list': 0.38; "couldn't": 0.39; 'visual': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'called': 0.40; 'how': 0.40; 'read': 0.60; 'expression': 0.60; 'most': 0.60; 'simple': 0.61; 'first': 0.61; 'teaching': 0.64; 'more': 0.64; 'forward': 0.65; 'within': 0.65; 'license': 0.66; 'line,': 0.68; 'default': 0.69; 'article': 0.77; 'answer:': 0.84; 'college,': 0.84; 'everywhere.': 0.84; 'afford': 0.91; 'taught': 0.96
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=5DVTBvws3bfG0VV3gDypnblJMKwzn85qQTvJmxywK+s=; b=nuH/lY4EVTDHPbIOIFqmKrt++hh0l89iMLHYWrXijwp8LxB2GNPwLjrvJhtLunIHJy nHMUPjUDquF89DwIMmVz9/mJmQvGsTDlV0aQmM59R7jBe1n5XT7qBXw284K854SvYdgE Ss7FcT0S8t3s0/LRDv7PTPajLcGutkcNeLvjniROyycS+G8JWxIriLspVy2I/KxjJ5dg zWLOeA5JFPeRvlkEsJBYljNpN4oOlIhAPCpDH3Do2u14Vq0J2YNtKuCSaGAKvp6AJ8Bx l1aV0ElLY5fRtSRVrXwZbr/93cDyd7hoM5P6ZidqIxKVc1sQkaAHeT7aIEQaaI61qP0l cvXQ==
X-Received by 10.180.208.35 with SMTP id mb3mr25394386wic.48.1405795226712; Sat, 19 Jul 2014 11:40:26 -0700 (PDT)
Date Sat, 19 Jul 2014 19:40:25 +0100
From Wojciech Giel <wojtekgiel@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: What's the proper style for a library string function?
References <53CAAD27.9010207@cdreimer.com>
In-Reply-To <53CAAD27.9010207@cdreimer.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Mailman-Approved-At Sun, 20 Jul 2014 00:05:34 +0200
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.12064.1405807535.18130.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1405807535 news.xs4all.nl 2883 [2001:888:2000:d::a6]:36533
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74838

Show key headers only | View raw


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

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


Thread

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

csiph-web