Tuple assignment and generators?

V

vdrab

You've been told that quite a few times before that "is" is not intended for
what you used it.

I got that. I was cleaning up some code that used "is" incorrectly
immediately after.
Some people actually listen to what others tell. Others seem to be driven by
the deep desire to make even the tiniest bit of getting-a-grasp a public
affair.

Not really. I always found python to be true to that -- admittedly
elusive -- principle of least surprise up to now ("special cases aren't
special enough to break the rules", maybe? I don't know. but then you
figured that, right?), and was thrown off quite a bit by the behaviour
described in one of the earlier posts, that is all. I wanted to ask
people's explanations about it and learnt a few things on the way
(thanks Dave). What did you get from all of this?
 
C

Carl Banks

vdrab said:
I guess the take-away lesson is to steer clear from any reliance on
object identity checks, if at all possible.
BINGO!


Are there any other such
"optimizations" one should like to know about?

You don't have to know about them, as long as you use the operators
correctly.

== tests equality. is tests identity. Use is ONLY when you are
testing whether two things are the same object. Otherwise, use ==.
When deciding which operator to use, ask yourself this: would the
result still be true if they were different objects with the same
value? If yes, then use ==. 0 == 0 should be true even if the two
zeros are different objects.

Corrollary:

You should test for singleton objects with is. None, NotImplemented,
and Ellipsis are singleton objects; this is part of the language and
not an implementation detail. You can rely on it.


Carl Banks
 
M

Mel Wilson

vdrab said:
I guess the take-away lesson is to steer clear from any reliance on
object identity checks, if at all possible. Are there any other such
"optimizations" one should like to know about?

Object identity checks are just the thing/numero uno/ichiban
for checking object identity. A code snipped like

def broadcast (self, message):
"Broadcast a message to all the other game players."
for p in all_players:
if p is not self:
p.send (message)

does just what I want and expect it to.

Mel.
 
J

John J. Lee

vdrab said:
In fact, I think my code contains things like "if len(arg) is 0:" and
so on,

So you made a mistake. It's OK, you can forgive yourself, nobody will
and I feel I should be able to do so given the way python treats
(claims to treat?) constant objects, even if I don't care whether the
values actually represent the same object.

(By "constant" I assume you mean immutable.)

I'm afraid it sounds like your assumptions about "the way python
treats (claims to treat?) constant objects" were "made up out of your
own head" as Tim Peters once put it :) Python does not make such
guarantees about the identity of separately-constructed immutable
objects, and that's good, careful design, in my book (you might be
able to see why if you think about what "is" *means* and what it would
take to ensure it *always* remains true for e.g. x is 10E9, where x ==
10E9 but is assigned elsewhere, and at another time during program
execution).

OTOH, it's good practice in Python to use "is" to compare values with
the immutable singleton None ("if x is None"). No harm comes of that,
simply because there is only one None object.


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,207
Latest member
KazukoCape

Latest Threads

Top