Python Basic Doubt
Chris Angelico
rosuav at gmail.com
Sun Aug 11 00:51:36 EDT 2013
On Sun, Aug 11, 2013 at 5:29 AM, Gary Herron
<gary.herron at islandtraining.com> wrote:
> A beginner, on his first program or two, can understand 1, and perhaps
> parrot 2 without understanding (or needing to). But the step from there to
> 3 is huge. It's folly to dump that on a first-time programmer. (It's
> probably even folly to dump that on a seasoned programmer just starting in
> Python. I still remember not understanding the explanation for "is" when I
> first read it. And it continued to make no sense until I had enough
> experience to understand the difference betwen C/C++ assignment to variables
> and Python's binding of variables.)
See, that's where the problem is. You will never grok the difference
between == and is if you're still thinking about C variables. (Though
you *might* be able to explain it by talking solely about char* and
the way two C strings can be the same but stored at different places
in memory. But that would be unhelpful most of the time.)
This is important *early* reading for a new Python programmer:
http://mail.python.org/pipermail/tutor/2010-December/080505.html
Note that it was originally posted on python-tutor, so it was
definitely aimed at the inexperienced.
> On 08/10/2013 08:43 PM, Chris Angelico wrote:
> Granted, English is a poor litmus test for code. But in this
> particular example, we're talking about immutable types (simple
> integers), where value and identity are practically the same. A Python
> implementation would be perfectly justified in interning *every*
> integer, in which case the 'is' would work perfectly here. The
> distinction between the two is important when the objects are mutable
> (so they have an identity that's distinct from their current values).
>
>
> Granted. But please note: There is *nothing* in that sentence which is fit
> for a beginner programmer. ... "immutable", "value/identity", "interning"
> ... In one ear and out the other. :-)
Right. This isn't my explanation of 'is' and '=='; it's my explanation
of why it's important to HAVE an explanation of the aforementioned. :)
Though the difference between value and identity is significant and
important, and mutability is bound to crop up fairly early on; so
really, it's only the concept of interning that would be really
advanced.
ChrisA
More information about the Python-list
mailing list