string slicing

Josiah Carlson jcarlson at uci.edu
Sun Dec 5 15:09:22 EST 2004


Ishwor <ishwor.gurung at gmail.com> wrote:
> 
> On Sun, 05 Dec 2004 10:31:12 -0800, Josiah Carlson <jcarlson at uci.edu> wrote:
> > 
> > "Fredrik Lundh" <fredrik at pythonware.com> wrote:
> 
> > > a suggestion: if you really want to be productive in python, forget about
> > > "is" for a while.
> 
> I know what Fredrik means here (Thanx Frederick :) ) but IMHO if the
> pattern of forgetting something is taken everytime someone tries to
> scratch under the hood, then seriously it'll take ages before someone
> seriously learns a language!
>  
> > The above two tips are in my "top-10 things to tell newbs" list.
> 
> You should consider having your own *nice* nifty things to say about
> newbs (like me) rather than try to point at someone else's
> intellectual property. If you are running out of ideas consider
> reading one of those "Complete Idiot's guide to xxxx" :)

Goodness, you got right snippy with the 'intellectual property' thing.
I'm sorry if it sounded as if I had meant "I came up with this before",
I did not mean it that way.  What I meant was "good advice", "I've seen
this advice before", and "I've previously taken note of it".  As an
aside, I also believe that if Fredrik were concerned about my supposed
theft of his 'advice IP', he would mention it


Confusions about 'is' are quite common.  Perhaps every week a new thread
in regards to object identity is offered, usually asking about integer,
string or list identity.  I'm not having much luck with the google
incantation right now, but my email archive is spitting back a thread on
"interning strings" from early November, which discusses strings and
identity.

Really, everyone who is thinking of using 'is' should ask themselves
whether or not it is truely important to have the same object.  In some
cases it matters, and in others cases it doesn't matter.  The more
experience one has with Python and the implementations of algorithms,
the more obvious such choices will become.

A perfect example of a case when using 'is' is proper is by Barry Warsaw
(http://mail.python.org/pipermail/python-dev/2004-March/043224.html):

missing = object()

if d.get('somekey', missing) is missing:
   # it ain't there


In the case of it not mattering, there was a series of threads in March
of this year in python-dev, discussing replacing the 'is' operator
strictly comparing object identity with a 'can be replaced by' operator,
spelled 'is'.  See the threads entitled "redefining is" here:
http://mail.python.org/pipermail/python-dev/2004-March/thread.html
Of course it came out that it didn't happen ('is' is much faster, and
doesn't require objects to define a mechanism to get at their contents,
etc.), but it is not uncommon for people who think they want 'is' to
really want 'can be replaced by', especially in the case of tuples:

>>> a = tuple(list('hello'))
>>> b = tuple(list('hello'))
>>> a == b
True
>>> a is b
False
>>> CanBeReplacedBy(a, b)
True
>>> 

(of course you need a proper implementation of CanBeReplacedBy, of which
a few are offered in the previously mentioned 'replacing is' threads).


Now, whether or not knowing what 'is' does, or whether knowing the
internals in regards to how object identity works, is necessary for
learning the language, I don't know.  It's been a few years since I was
a newb, but considering all of the questions regarding 'is' usually
resulting with python-list saying "you want '=='", I'd offer, 'it is not
necessary'.


 - Josiah




More information about the Python-list mailing list