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


Groups > comp.lang.python > #66088

Re: How does python know?

Date 2014-02-12 13:02 -0800
From Gary Herron <gary.herron@islandtraining.com>
Subject Re: How does python know?
References <lFQKu.455927$cZ.440055@fx31.iad>
Newsgroups comp.lang.python
Message-ID <mailman.6786.1392239501.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 02/12/2014 12:17 PM, Tobiah wrote:
> I do this:
>
> a = 'lasdfjlasdjflaksdjfl;akjsdf;kljasdl;kfjasl'
> b = 'lasdfjlasdjflaksdjfl;akjsdf;kljasdl;kfjasl'
>
> print
> print id(a)
> print id(b)
>
>
> And get this:
>
> True
> 140329184721376
> 140329184721376
>
>
> 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?
>
> Thanks,
>
> Tobiah

Yes.

Kind of:  It's a hash calculation not a direct comparison, and it's 
applied to strings  of limited length.  Details are implementation (and 
perhaps version) specific.  The process is called "string interning".  
Google and wikipedia have lots to say about it.

Gary Herron

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