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


Groups > comp.lang.python > #66076 > unrolled thread

How does python know?

Started byTobiah <toby@tobiah.org>
First post2014-02-12 12:17 -0800
Last post2014-02-12 20:54 -0500
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#66076 — How does python know?

FromTobiah <toby@tobiah.org>
Date2014-02-12 12:17 -0800
SubjectHow does python know?
Message-ID<lFQKu.455927$cZ.440055@fx31.iad>
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

[toc] | [next] | [standalone]


#66078

FromTobiah <toby@tobiah.org>
Date2014-02-12 12:27 -0800
Message-ID<pOQKu.37378$vk5.11310@fx03.iad>
In reply to#66076
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

Weird as well, is that in the interpreter,
the introduction of punctuation appears to
defeat the reuse of the object:

 >>> b = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
 >>> a = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
 >>> a is b
True
 >>> a = 'la;sdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
 >>> b = 'la;sdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
 >>> a is b
False
 >>> b = 'la.sdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
 >>> a = 'la.sdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
 >>> a is b
False
 >>> a = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
 >>> b = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
 >>> a is b
True

Tobiah

[toc] | [prev] | [next] | [standalone]


#66101

FromDave Angel <davea@davea.name>
Date2014-02-12 16:59 -0500
Message-ID<mailman.6797.1392242168.18130.python-list@python.org>
In reply to#66078
 Tobiah <toby@tobiah.org> Wrote in message:
> 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
> 
> Weird as well, is that in the interpreter,
> the introduction of punctuation appears to
> defeat the reuse of the object:
> 
>  >>> b = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
>  >>> a = 'lasdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
>  >>> a is b
> True
>  >>> a = 'la;sdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
>  >>> b = 'la;sdfjlasdjflaksdjflakjsdfkljasdlkfjasl'
>  >>> a is b
> 
> 


As others have said, interning is implementation specific, so you
 should never rely on it. 

I think the current CPython algorithm is designed to save both
 memory and time in the storage and look up of symbol names.  In a
 typical program, those are the most likely to be duplicated.  So
 if your literal is of reasonable size and doesn’t invalid symbol
 characters (such as space, punctuation,  etc) then it just might
 be added to the interned dictionary. 

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#66079

FromChris Angelico <rosuav@gmail.com>
Date2014-02-13 07:33 +1100
Message-ID<mailman.6779.1392237224.18130.python-list@python.org>
In reply to#66076
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

[toc] | [prev] | [next] | [standalone]


#66088

FromGary Herron <gary.herron@islandtraining.com>
Date2014-02-12 13:02 -0800
Message-ID<mailman.6786.1392239501.18130.python-list@python.org>
In reply to#66076
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

[toc] | [prev] | [next] | [standalone]


#66133

FromRoy Smith <roy@panix.com>
Date2014-02-12 20:54 -0500
Message-ID<roy-98A4D3.20542312022014@news.panix.com>
In reply to#66076
In article <lFQKu.455927$cZ.440055@fx31.iad>, Tobiah <toby@tobiah.org> 
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?

Yes[*].  It's called interning.  See 
https://en.wikipedia.org/wiki/Intern_(computer_science).

[*] Well, nothing requires Python to do that.  Some implementations do.  
Some don't.  Some do it for certain types of strings.  Your mileage may 
vary.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web