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


Groups > comp.lang.python > #66079

Re: How does python know?

References <lFQKu.455927$cZ.440055@fx31.iad>
Date 2014-02-13 07:33 +1100
Subject Re: How does python know?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.6779.1392237224.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Feb 13, 2014 at 7:17 AM, Tobiah <toby@tobiah.org> wrote:
> This works for longer strings.  Does python
> compare a new string to every other string
> I've made in order to determine whether it
> needs to create a new object?

No, it doesn't; but when you compile a module (including a simple
script like that), Python checks for repeated literals. It's only good
for literals, though.

If you specifically need this behaviour, it's called 'interning'. You
can ask Python to do this, or you can do it manually. But most of the
time, you can just ignore id() and simply let two strings be equal
based on their contents; the fact that constants are shared is a neat
optimization, nothing more.

ChrisA

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


Thread

How does python know? Tobiah <toby@tobiah.org> - 2014-02-12 12:17 -0800
  Re: How does python know? Tobiah <toby@tobiah.org> - 2014-02-12 12:27 -0800
    Re: How does python know? Dave Angel <davea@davea.name> - 2014-02-12 16:59 -0500
  Re: How does python know? Chris Angelico <rosuav@gmail.com> - 2014-02-13 07:33 +1100
  Re: How does python know? Gary Herron <gary.herron@islandtraining.com> - 2014-02-12 13:02 -0800
  Re: How does python know? Roy Smith <roy@panix.com> - 2014-02-12 20:54 -0500

csiph-web