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


Groups > comp.lang.python > #93851

Re: Improve usage of Python 3

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'newline': 0.07; 'omit': 0.07; 'cc:addr:python-list': 0.09; 'args,': 0.09; 'argument,': 0.09; 'semantics': 0.09; 'python': 0.10; 'output': 0.13; 'syntax': 0.13; 'def': 0.13; 'wed,': 0.15; 'things.': 0.15; '"*"': 0.16; '2:28': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'lambda': 0.16; 'magic': 0.16; 'parentheses': 0.16; 'start?': 0.16; 'statement.': 0.16; 'subject:usage': 0.16; 'wrote:': 0.16; 'else,': 0.18; 'string,': 0.18; 'language': 0.19; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'simpler': 0.22; 'pass': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'define': 0.27; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'directly,': 0.29; 'separated': 0.29; 'spaces': 0.29; 'print': 0.30; '15,': 0.30; 'mention': 0.30; 'good.': 0.32; 'maintaining': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'mix': 0.35; 'skip:> 10': 0.35; 'something': 0.35; 'but': 0.36; 'project': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'doing': 0.38; 'where': 0.40; 'space': 0.40; 'easy': 0.60; 'elsewhere': 0.66; "they're": 0.66; 'worth': 0.67; 'teach': 0.70; 'jul': 0.72; 'chrisa': 0.84; 'world!"': 0.84; 'to:none': 0.91; 'refuse': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=z/Y7dULOkWKWcx9xQ3DaoNxhEoNo5eOWy5gRieV3MVE=; b=0KWjJerUfRLf85PcQgKkcqiU50bv73w0Dmi4liwMxZj5m3Nf2LpURUJYnDzO1aXhfx eFG8w46SLoRVxXpcb3VXEp/E6ciMvp6LSCunXf+6ONE9v79ZWRtIQYSX8IJxS8JsUlkg yxXAGDCIPslmeKa/8kkc/mFDZGeak+FwaoNU0KFLSQFAa1CoI+dWAza7JuS1ziDCuiJ0 NjyB2Hipkm5haUR5j/ppDYjE32wkSYCW4AJfiHlf1QilqtTnrB/E0cxYTPn3v+B+dpva JF0n2hQSMuymCXj0KzShxZXCkpbReocPkl3r76T3wUAF0l4YISk3ERdz82caytF7eq8m vCNA==
MIME-Version 1.0
X-Received by 10.50.29.75 with SMTP id i11mr24103432igh.50.1436946015049; Wed, 15 Jul 2015 00:40:15 -0700 (PDT)
In-Reply-To <55A538C9.5050509@gmail.com>
References <55A538C9.5050509@gmail.com>
Date Wed, 15 Jul 2015 17:40:14 +1000
Subject Re: Improve usage of Python 3
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
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.528.1436946023.3674.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1436946023 news.xs4all.nl 2889 [2001:888:2000:d::a6]:47427
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:93851

Show key headers only | View raw


On Wed, Jul 15, 2015 at 2:28 AM, Marcos <bugshideout@gmail.com> wrote:
> And incredibly, there are a few users on the same project who refuse to use
> python 3 simply because of the print statement.

Solution: Explain to them the massive benefits of the print function.
It may be simpler to omit the parentheses in the case where you just
want to print out a single string, but for pretty much everything
else, the function is at least as good.

# I can never remember the syntax for this, and
# have to look it up every time. Can you put the
# file reference at the end? Or only at the start?
print >>sys.stderr, "Hello, world!"

# Keyword-only argument, exactly the way every
# other Python function operates
print("Hello, world!", file=sys.stderr)

# Separated by spaces
print "Hello", "world", "!"

# Separated by spaces
print("Hello", "world", "!")

# Separated by something other than space
# Again, keyword-only args, nice and easy
print("Hello", "world", "!", sep="*")

# No newline at the end: magic
print "Hello, world!",
# Hard to explain the exact semantics of the
# soft space and what happens if you mix
# print and sys.stdout.write(), not to mention
# sys.stderr.write() when they're both attached
# to the same console

# No newline at the end: no magic
print("Hello, world!", end="")

# Need to define a shim function to pass elsewhere
def Print(x):
    print x
walk(tree, Print)
def print_with_star(x):
    print x, "*"
walk(tree, print_with_star)

# Can just use print directly, or use lambda
walk(tree, print)
walk(tree, lambda x: print(x, end="*\n"))

The function is well worth it. Teach people the new way, don't try to
warp the language around maintaining an old way of doing things.
There's really no reason for console output to have magic syntax.

ChrisA

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


Thread

Re: Improve usage of Python 3 Chris Angelico <rosuav@gmail.com> - 2015-07-15 17:40 +1000

csiph-web