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


Groups > comp.lang.python > #66262

Re: A curious bit of code...

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'string.': 0.05; 'string': 0.09; 'bold': 0.09; 'oh,': 0.09; 'cc:addr:python-list': 0.11; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'literals': 0.16; 'slow,': 0.16; 'string:': 0.16; 'subject:bit': 0.16; 'wrote:': 0.18; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'propose': 0.24; 'skip:l 30': 0.24; 'cc:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'that.': 0.31; 'noted': 0.31; 'fri,': 0.33; 'skip:s 30': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'edge': 0.36; 'doing': 0.36; 'changing': 0.37; 'two': 0.37; 'handle': 0.38; 'capital': 0.73; 'ethan': 0.84; 'furman': 0.84; 'to:none': 0.92
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=DSHCR9ZXwiGYPCDBPlTOiDP3xohP7Bp/n2JaukWuKCY=; b=i5EudRPSZ63u4u+klsi+2gqEsfVRDkRthMK9GZNyZmHfbPKk8wgwBPeyThTRqCW4gJ DJEwjZgTSOlUoiypPF+WftJ+q+0eNH6sfOGCoaLNTAGmECTvfZezU1cR1vG8sFIg3av0 noXMzKZyd06ZRmZ0V9eEn8+hafj4exqoxcL2J9UjXOCSYgzGlVq2Gc4OtsSkEucRpEA7 M9u/1P5Bn6q/dI2xOHY0s0aQnv+t+uiN1reIIobWxpPUQQv0TD1Cxz9ywJjc/oMFuMTf bh77lH1CrJUB4+UkP0j+h62l7fjGzs50Q7e2OjT3E/DwYGd0l1De1mCCDkE8HTucBUC1 QFIw==
MIME-Version 1.0
X-Received by 10.66.118.71 with SMTP id kk7mr4645878pab.14.1392329608920; Thu, 13 Feb 2014 14:13:28 -0800 (PST)
In-Reply-To <52FD3A3B.1060300@stoneleaf.us>
References <4cc09129-43ee-4205-a24c-03f92b594abc@googlegroups.com> <CAKJDb-OoN2K4--AseaFkuXv3-4vr+a3TPQc7omSrsHVYKcbBrw@mail.gmail.com> <ldjbft$af$1@ger.gmane.org> <CAPTjJmpi5HsKgAW7axTbNY5_3XeZqeeH4CsATz_6Rf0_2MgAMw@mail.gmail.com> <52FD3A3B.1060300@stoneleaf.us>
Date Fri, 14 Feb 2014 09:13:28 +1100
Subject Re: A curious bit of code...
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.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.6896.1392330021.18130.python-list@python.org> (permalink)
Lines 21
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1392330021 news.xs4all.nl 2869 [2001:888:2000:d::a6]:47125
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66262

Show key headers only | View raw


On Fri, Feb 14, 2014 at 8:33 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
> Oh, it's not that bad!  All you have to do is handle the edge case of an
> empty string:
>
> s[::len(s)-1 if s else True]
>
> *ducks and runs*

And the edge case of the one-character string. Also, it's been noted
that calling the built-in function len() is slow, so I propose
changing that.

s[::(sys.getsizeof("\N{MATHEMATICAL BOLD CAPITAL
L}"+s)-sys.getsizeof("\N{MATHEMATICAL BOLD CAPITAL E}\N{MATHEMATICAL
BOLD CAPITAL N}") or 4)//4]

Note the elegance of using the word LEN across two string literals
(because string literals are fast) as a means of clearly describing
what we are doing - an optimization of the built-in len() function.

ChrisA

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


Thread

A curious bit of code... forman.simon@gmail.com - 2014-02-13 10:37 -0800
  Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 13:45 -0500
  Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 10:45 -0800
  Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-13 19:09 +0000
  Re: A curious bit of code... Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-02-13 20:05 +0100
  Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 19:17 +0000
  Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:20 -0800
    Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 14:28 -0500
  Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:25 -0800
  Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 19:25 +0000
  Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-13 19:32 +0000
  Re: A curious bit of code... Peter Otten <__peter__@web.de> - 2014-02-13 20:43 +0100
    Re: A curious bit of code... Marko Rauhamaa <marko@pacujo.net> - 2014-02-13 21:56 +0200
  Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:23 -0800
  Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 19:51 +0000
  Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 11:59 -0800
  Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 13:59 -0600
  Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 07:29 +1100
  Re: A curious bit of code... Tim Chase <python.list@tim.thechases.com> - 2014-02-13 14:39 -0600
  Re: A curious bit of code... Emile van Sebille <emile@fenx.com> - 2014-02-13 12:55 -0800
    Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 16:24 -0500
      Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 16:23 -0800
  Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 08:01 +1100
  Re: A curious bit of code... Neil Cerutti <neilc@norwich.edu> - 2014-02-13 21:01 +0000
  Re: A curious bit of code... Peter Otten <__peter__@web.de> - 2014-02-13 22:06 +0100
  Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 08:10 +1100
  Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-13 21:14 +0000
  Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 15:20 -0600
  Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 15:19 -0600
  Re: A curious bit of code... Emile van Sebille <emile@fenx.com> - 2014-02-13 13:23 -0800
  Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 08:31 +1100
    Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 16:38 -0500
  Re: A curious bit of code... Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-13 15:47 -0600
  Re: A curious bit of code... Serhiy Storchaka <storchaka@gmail.com> - 2014-02-13 23:49 +0200
    Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-13 16:51 -0500
  Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 13:33 -0800
  Re: A curious bit of code... Chris Angelico <rosuav@gmail.com> - 2014-02-14 09:13 +1100
  Re: A curious bit of code... Ethan Furman <ethan@stoneleaf.us> - 2014-02-13 14:26 -0800
  Re: A curious bit of code... Terry Reedy <tjreedy@udel.edu> - 2014-02-13 19:29 -0500
  Re: A curious bit of code... forman.simon@gmail.com - 2014-02-13 18:45 -0800
    Re: A curious bit of code... Ned Batchelder <ned@nedbatchelder.com> - 2014-02-13 22:26 -0500
      Re: A curious bit of code... forman.simon@gmail.com - 2014-02-14 12:04 -0800
        Re: A curious bit of code... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-14 21:01 +0000
  Re: A curious bit of code... Dave Angel <davea@davea.name> - 2014-02-14 07:19 -0500
    Re: A curious bit of code... Roy Smith <roy@panix.com> - 2014-02-14 09:06 -0500

csiph-web