From 28fa624a88034448707aa9457317f34e3c56200a Mon Sep 17 00:00:00 2001 From: Jonathan H Date: Sun, 25 Feb 2018 17:57:04 +0000 Subject: [PATCH 1/2] Proposal for issue #67 --- CONTRIBUTORS.md | 1 + README.md | 8 +++++--- wtfpython-pypi/content.md | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 21ad71c..41f7d0b 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -9,5 +9,6 @@ I'm really grateful to all the contributors. Following are the wonderful people | tukkek | [tukkek](https://github.com/tukkek) | [#11](https:/github.com/satwikkansal/wtfpython/issues/11), [#26](https:/github.com/satwikkansal/wtfpython/issues/26) | | PiaFraus | [PiaFraus](https://github.com/PiaFraus) | [#9](https:/github.com/satwikkansal/wtfpython/issues/9) | | chris-rands | [chris-rands](https://github.com/chris-rands) | [#32](https:/github.com/satwikkansal/wtfpython/issues/32) | +| Jonathan H | [sheljohn](https://github.com/sheljohn) | [#67](https://github.com/satwikkansal/wtfpython/issues/67) | Thank you all for taking out time, and helping to make this project awesome! :smile: diff --git a/README.md b/README.md index 5ca25a3..2481515 100644 --- a/README.md +++ b/README.md @@ -194,12 +194,14 @@ False Makes sense, right? -#### 💡 Explanation: -+ Such behavior is due to CPython optimization (called string interning) that tries to use existing immutable objects in some cases rather than creating a new object every time. +#### 💡 Explanatios: + ++ Such behavior is due to CPython optimization (called **string interning**) that tries to use existing immutable objects in some cases rather than creating a new object every time. ++ Note that the keyword `is` is used for _reference_ equality (unlike `==` which is used for _value_ equality); this means that `a is b` is the same as `id(a) == id(b)`, and therefore it should not be used for string comparison. It is only used here to illustrate the effects of string interning. + After being interned, many variables may point to the same string object in memory (thereby saving memory). + In the snippets above, strings are implicitly interned. The decision of when to implicitly intern a string is implementation dependent. There are some facts that can be used to guess if a string will be interned or not: * All length 0 and length 1 strings are interned. - * Strings are interned at compile time (`'wtf'` will be interned but `''.join(['w', 't', 'f']` will not be interned) + * Strings are interned at compile time (`'wtf'` will be interned but `''.join(['w', 't', 'f']` will not be interned). * Strings that are not composed of ASCII letters, digits or underscores, are not interned. This explains why `'wtf!'` was not interned due to `!`. + When `a` and `b` are set to `"wtf!"` in the same line, the Python interpreter creates a new object, then references the second variable at the same time. If you do it on separate lines, it doesn't "know" that there's already `wtf!` as an object (because `"wtf!"` is not implicitly interned as per the facts mentioned above). It's a compiler optimization and specifically applies to the interactive environment. diff --git a/wtfpython-pypi/content.md b/wtfpython-pypi/content.md index 3fd8a13..70a9a95 100644 --- a/wtfpython-pypi/content.md +++ b/wtfpython-pypi/content.md @@ -556,10 +556,11 @@ Makes sense, right? #### 💡 Explanation: + Such behavior is due to CPython optimization (called string interning) that tries to use existing immutable objects in some cases rather than creating a new object every time. ++ Note that the keyword `is` is used for _reference_ equality (unlike `==` which is used for _value_ equality); this means that `a is b` is the same as `id(a) == id(b)`, and therefore it should not be used for string comparison. It is only used here to illustrate the effects of string interning. + After being interned, many variables may point to the same string object in memory (thereby saving memory). + In the snippets above, strings are implicitly interned. The decision of when to implicitly intern a string is implementation dependent. There are some facts that can be used to guess if a string will be interned or not: * All length 0 and length 1 strings are interned. - * Strings are interned at compile time (`'wtf'` will be interned but `''.join(['w', 't', 'f']` will not be interned) + * Strings are interned at compile time (`'wtf'` will be interned but `''.join(['w', 't', 'f']` will not be interned). * Strings that are not composed of ASCII letters, digits or underscores, are not interned. This explains why `'wtf!'` was not interned due to `!`. + When `a` and `b` are set to `"wtf!"` in the same line, the Python interpreter creates a new object, then references the second variable at the same time. If you do it on separate lines, it doesn't "know" that there's already `wtf!` as an object (because `"wtf!"` is not implicitly interned as per the facts mentioned above). It's a compiler optimization and specifically applies to the interactive environment. From c84b5169d8872af232136314860e5d9bc0ca54fc Mon Sep 17 00:00:00 2001 From: Jonathan H Date: Sun, 25 Feb 2018 18:00:39 +0000 Subject: [PATCH 2/2] remove lineskip and bolden string interning in contents.md --- README.md | 1 - wtfpython-pypi/content.md | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 2481515..b649bd3 100644 --- a/README.md +++ b/README.md @@ -195,7 +195,6 @@ False Makes sense, right? #### 💡 Explanatios: - + Such behavior is due to CPython optimization (called **string interning**) that tries to use existing immutable objects in some cases rather than creating a new object every time. + Note that the keyword `is` is used for _reference_ equality (unlike `==` which is used for _value_ equality); this means that `a is b` is the same as `id(a) == id(b)`, and therefore it should not be used for string comparison. It is only used here to illustrate the effects of string interning. + After being interned, many variables may point to the same string object in memory (thereby saving memory). diff --git a/wtfpython-pypi/content.md b/wtfpython-pypi/content.md index 70a9a95..ad35e0c 100644 --- a/wtfpython-pypi/content.md +++ b/wtfpython-pypi/content.md @@ -555,7 +555,7 @@ False Makes sense, right? #### 💡 Explanation: -+ Such behavior is due to CPython optimization (called string interning) that tries to use existing immutable objects in some cases rather than creating a new object every time. ++ Such behavior is due to CPython optimization (called **string interning**) that tries to use existing immutable objects in some cases rather than creating a new object every time. + Note that the keyword `is` is used for _reference_ equality (unlike `==` which is used for _value_ equality); this means that `a is b` is the same as `id(a) == id(b)`, and therefore it should not be used for string comparison. It is only used here to illustrate the effects of string interning. + After being interned, many variables may point to the same string object in memory (thereby saving memory). + In the snippets above, strings are implicitly interned. The decision of when to implicitly intern a string is implementation dependent. There are some facts that can be used to guess if a string will be interned or not: