Created
August 1, 2014 02:50
-
-
Save scode/ba6f7395ae7c039b4c75 to your computer and use it in GitHub Desktop.
Python is insane
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Python 2.7.6 (default, Mar 22 2014, 22:59:56) | |
| [GCC 4.8.2] on linux2 | |
| Type "help", "copyright", "credits" or "license" for more information. | |
| >>> l1 = [1, 2] | |
| >>> l2 = l1 | |
| >>> l1 | |
| [1, 2] | |
| >>> l2 | |
| [1, 2] | |
| >>> l1 += [3] | |
| >>> l1 | |
| [1, 2, 3] | |
| >>> l2 | |
| [1, 2, 3] | |
| >>> l1 = l1 + [4] | |
| >>> l1 | |
| [1, 2, 3, 4] | |
| >>> l2 | |
| [1, 2, 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use of the += operator invokes the type class'
__iadd__()method which modifies in-place by reference (and retains the modified reference). mutable types do not implement__iadd__(as they cannot modify in-place), in which case the += handler falls back to__add__and returns a reference to the new value.illustrating
__iadd__and__add__behaviorbehavior for immutable string type (invokes
__add__as illustrated above):