473,542 Members | 2,829 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"is" and ==

Can someone please explain to me the difference between the "is"
keyword and the == boolean operator. I can't figure it out on my own
and I can't find any documentation on it.

I can't understand why this works:

if text is None:

and why this always returns false:

if message is 'PING':

even when message = 'PING'.

What's the deal with that?

May 30 '07 #1
81 3545
BlueJ774 wrote:
Can someone please explain to me the difference between the "is"
keyword and the == boolean operator. I can't figure it out on my own
and I can't find any documentation on it.

I can't understand why this works:

if text is None:

and why this always returns false:

if message is 'PING':

even when message = 'PING'.

What's the deal with that?
`x is y` means the same thing as:

id(x) == id(y)

You use the `is` operator for when you're testing for _object identity_,
not value. `None` is a special object sentinel that is not only a value
but a special _object_, and so if you're testing whether or not an
object is `None`, you do so with the `is` operator.

If you're testing whether an object is equal to the string "PING" then
you do not want to do so by identity, but rather value, so you use the
`==` operator, not `is`.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
You could have another fate / You could be in another place
-- Anggun
May 30 '07 #2
On May 30, 12:57 am, Erik Max Francis <m...@alcyone.c omwrote:
BlueJ774 wrote:
Can someone please explain to me the difference between the "is"
keyword and the == boolean operator. I can't figure it out on my own
and I can't find any documentation on it.
I can't understand why this works:
if text is None:
and why this always returns false:
if message is 'PING':
even when message = 'PING'.
What's the deal with that?

`x is y` means the same thing as:

id(x) == id(y)

You use the `is` operator for when you're testing for _object identity_,
not value. `None` is a special object sentinel that is not only a value
but a special _object_, and so if you're testing whether or not an
object is `None`, you do so with the `is` operator.

If you're testing whether an object is equal to the string "PING" then
you do not want to do so by identity, but rather value, so you use the
`==` operator, not `is`.

--
Erik Max Francis && m...@alcyone.co m &&http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
You could have another fate / You could be in another place
-- Anggun
Thanks. That's exactly what I was looking for.

May 30 '07 #3
I want to call every object in a tupple, like so:

#------------------------------------------
def a: print 'a'
def b: print 'b'
c = (a,b)
>>>c[:]() # i wanna
TypeError: 'tupple' object is not callable
>>>c[0]() # expected
a
>>>c[:][0] # huh?
a
>>[i() for i in c] # too long and ...huh?
a
b
[None,None]
#------------------------------------------

Why? Because I want to make Python calls from a cell phone.
Every keystroke is precious; even list comprehension is too much.

Is there something obvious that I'm missing?

Warren

Today's quote: "HELLO PARROT! wakey wakey!"

May 30 '07 #4
On Wed, 2007-05-30 at 11:48 -0700, Warren Stringer wrote:
I want to call every object in a tupple, like so:

#------------------------------------------
def a: print 'a'
def b: print 'b'
c = (a,b)
>>c[:]() # i wanna
[...]
Is there something obvious that I'm missing?
Yes: Python is not Perl.

Python is based on the principle that programmers don't write computer
code for the benefit of the computer, but for the benefit of any
programmer who has to read their code in the future. Terseness is not a
virtue. To call every function in a tuple, do the obvious:

for func in funcs: func()

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
May 30 '07 #5
Hmmm, this is for neither programmer nor computer; this is for a user. If I
wanted to write code for the benefit for the computer, I'd still be flipping
switches on a PDP-8. ;-)

This is inconsistent:

why does c[:][0]() work but c[:]() does not?
Why does c[0]() has exactly the same results as c[:][0]() ?
Moreover, c[:][0]() implies that a slice was invoked

So, tell me, for scheduling a lot of asynchronous events, what would be more
readable than this:

bidders = [local_members] + [callin_members]
bidders[:].sign_in(roster )
...

\~/
-----Original Message-----
From: py************* *************** *****@python.or g [mailto:python-list-
bo************* ********@python .org] On Behalf Of Carsten Haese
Sent: Wednesday, May 30, 2007 12:55 PM
To: py*********@pyt hon.org
Subject: Re: c[:]()

On Wed, 2007-05-30 at 11:48 -0700, Warren Stringer wrote:
I want to call every object in a tupple, like so:

#------------------------------------------
def a: print 'a'
def b: print 'b'
c = (a,b)
>>>c[:]() # i wanna
[...]
Is there something obvious that I'm missing?

Yes: Python is not Perl.

Python is based on the principle that programmers don't write computer
code for the benefit of the computer, but for the benefit of any
programmer who has to read their code in the future. Terseness is not a
virtue. To call every function in a tuple, do the obvious:

for func in funcs: func()

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
--
http://mail.python.org/mailman/listinfo/python-list

May 30 '07 #6
On May 31, 12:31 am, "Warren Stringer" <war...@muse.co mwrote:
This is inconsistent:

why does c[:][0]() work but c[:]() does not?
Why does c[0]() has exactly the same results as c[:][0]() ?
Moreover, c[:][0]() implies that a slice was invoked
It's not inconsistent, but [:] probably does something different than
you think it does. All it does is create a copy (not in general, but
at least if c is a list or a tuple). Since in your example c is a
tuple and tuples are immutable, making a copy of it is essentially
useless. Why not just use the original? I.e. instead of c[:] you could
just write c. That's why c[:][0]() has exactly the same effect as c[0]
(), although the former is likely to be slightly slower.

c[:]() tries to call the copied tuple. Tuples aren't callable.
c[:][0]() calls the first element in the copied tuple, and that
element happens to be callable.

May 30 '07 #7
Warren Stringer said unto the world upon 05/30/2007 05:31 PM:
Hmmm, this is for neither programmer nor computer; this is for a user. If I
wanted to write code for the benefit for the computer, I'd still be flipping
switches on a PDP-8. ;-)

This is inconsistent:

why does c[:][0]() work but c[:]() does not?
c[:][0]() says take a copy of the list c, find its first element, and
call it. Since c is a list of functions, that calls a function.

c[:]() says take a copy of the list c and call it. Since lists are not
callable, that doesn't work.
Why does c[0]() has exactly the same results as c[:][0]() ?
Because c[0] is equal to c[:][0].
Moreover, c[:][0]() implies that a slice was invoked
Yes, but the complete slice.
So, tell me, for scheduling a lot of asynchronous events, what would be more
readable than this:

bidders = [local_members] + [callin_members]
bidders[:].sign_in(roster )
...
for bidder in [local_members] + [callin_members]:
bidder.sign_in( roster)

Best,

Brian vdB
\~/
>-----Original Message-----
From: py************* *************** *****@python.or g [mailto:python-list-
bo************ *********@pytho n.org] On Behalf Of Carsten Haese
Sent: Wednesday, May 30, 2007 12:55 PM
To: py*********@pyt hon.org
Subject: Re: c[:]()

On Wed, 2007-05-30 at 11:48 -0700, Warren Stringer wrote:
>>I want to call every object in a tupple, like so:

#------------------------------------------
def a: print 'a'
def b: print 'b'
c = (a,b)

>c[:]() # i wanna
[...]
Is there something obvious that I'm missing?
Yes: Python is not Perl.

Python is based on the principle that programmers don't write computer
code for the benefit of the computer, but for the benefit of any
programmer who has to read their code in the future. Terseness is not a
virtue. To call every function in a tuple, do the obvious:

for func in funcs: func()

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
--
http://mail.python.org/mailman/listinfo/python-list

May 30 '07 #8
Hey many thanks for the replies!

Ah, so is seems that c[:][:][:][:][:][:][:][:][:][:][:][0]()
also work ...

Ah well, can't have everything. Guess I was inspired by the alphabetically
adjacent message "Call for Ruby Champion". Would have been nice for it work
- a more elegant iterator would be hard to come by. A PEP, perhaps? Maybe
not; am still a bit new - am probably missing something obvious why this
isn't an easy fix.

Pretty cool that I can override the list class.

Cheers,

\~/
-----Original Message-----
From: py************* *************** *****@python.or g [mailto:python-list-
bo************* ********@python .org] On Behalf Of Brian van den Broek
Sent: Wednesday, May 30, 2007 3:00 PM
To: py*********@pyt hon.org
Subject: Re: c[:]()

Warren Stringer said unto the world upon 05/30/2007 05:31 PM:
Hmmm, this is for neither programmer nor computer; this is for a user.
If I
wanted to write code for the benefit for the computer, I'd still be
flipping
switches on a PDP-8. ;-)

This is inconsistent:

why does c[:][0]() work but c[:]() does not?

c[:][0]() says take a copy of the list c, find its first element, and
call it. Since c is a list of functions, that calls a function.

c[:]() says take a copy of the list c and call it. Since lists are not
callable, that doesn't work.
Why does c[0]() has exactly the same results as c[:][0]() ?

Because c[0] is equal to c[:][0].
Moreover, c[:][0]() implies that a slice was invoked

Yes, but the complete slice.
So, tell me, for scheduling a lot of asynchronous events, what would be
more
readable than this:

bidders = [local_members] + [callin_members]
bidders[:].sign_in(roster )
...

for bidder in [local_members] + [callin_members]:
bidder.sign_in( roster)

Best,

Brian vdB
\~/
-----Original Message-----
From: py************* *************** *****@python.or g [mailto:python-
list-
bo************* ********@python .org] On Behalf Of Carsten Haese
Sent: Wednesday, May 30, 2007 12:55 PM
To: py*********@pyt hon.org
Subject: Re: c[:]()

On Wed, 2007-05-30 at 11:48 -0700, Warren Stringer wrote:
I want to call every object in a tupple, like so:

#------------------------------------------
def a: print 'a'
def b: print 'b'
c = (a,b)

c[:]() # i wanna
[...]
Is there something obvious that I'm missing?
Yes: Python is not Perl.

Python is based on the principle that programmers don't write computer
code for the benefit of the computer, but for the benefit of any
programmer who has to read their code in the future. Terseness is not a
virtue. To call every function in a tuple, do the obvious:

for func in funcs: func()

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
--
http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list

May 30 '07 #9
On May 30, 5:37 pm, "Warren Stringer" <war...@muse.co mwrote:
Hey many thanks for the replies!

Ah, so is seems that c[:][:][:][:][:][:][:][:][:][:][:][0]()
also work ...

Ah well, can't have everything. Guess I was inspired by the alphabetically
adjacent message "Call for Ruby Champion". Would have been nice for it work
- a more elegant iterator would be hard to come by. A PEP, perhaps? Maybe
not; am still a bit new - am probably missing something obvious why this
isn't an easy fix.

Pretty cool that I can override the list class.

Cheers,
Do a search on "python is not java" (words to live by). You can also
plug in another language you know (like Ruby), but you won't get as
many results.

May 30 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1339
by: Bjorn | last post by:
Hi, I need to determine the hightest date between e.g. "8/3/2004" and "8/4/2004". With this code, i get 'dat2' , which means that "8/4/2004" is higher than "8/3/2004". That 's right. But with "8/3/2004" and from "8/10/2004" (also "8/11/2004" ...) i get 'dat1'. <% dat1="8/3/2004"
3
2111
by: Phil | last post by:
Hi everybody, I am a XSLT beginner and the following problem really makes me crazy ! I have a main "contacts.xml" document which contains references to several contact data XML files. My aim is to process the contacts in a single-pass XSLT process. That is why the "document()" function is what I need. I call the "document()" XPath...
3
12343
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
11
2073
by: Joseph Turian | last post by:
Fellow hackers, I have a class BuildNode that inherits from class Node. Similarly, I have a class BuildTree that inherits from class Tree. Tree includes a member variable: vector<Node> nodes; // For clarity, let this be "orig_nodes" BuildTree includes a member variable:
24
3573
by: hjbortol | last post by:
Hi! Is the expression "a >= b" equivalent to "a - b >= 0" in C/C++? Is this equivalence an IEEE/ANSI rule? Or is this machine/compiler dependent? Any references are welcome! Thanks in advance, Humberto.
15
2533
by: Bobby C. Jones | last post by:
Is there an advantage to either of these methods, or is it simply a matter of style? I used to use the "as" method, but later gave it up for the "is" method as the intent of the code seems a little clearer to me. I now have some projects coming up where the baseTypeCollection will contain 5k to 10k objects that will have to be searched for...
37
3897
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as a function
1
2262
by: craigkenisston | last post by:
I can't believe what is happening on my computer right now. I have a web project, file-system based on something like c:\Projects\ProjectX\www\. I had to make some changes and testing in a separate new directory so I created c:\Projects\ProjectX\www2\ copied all the content and build. I made the changes I wanted and test, everything worked...
9
4014
by: Dullme | last post by:
i can hardly know what is the code for this. I have researched palindromes, but unfortunately, I only have read about integers not for characters..I need some help..
8
12929
by: Zytan | last post by:
In VB, you use ControlChars.CrLf (or better, ControlChars.NewLine). In C/C++, we are used to using merely "\n" (or at least I am). It appears "\n" is good enough for RichTextBoxes. Does it matter? I am so used to typing "\n" that I don't think this habit will change anyday soon. Zytan
0
7395
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7333
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5250
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3382
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3383
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
624
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.