473,811 Members | 3,026 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 3677
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
1349
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
2127
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 function from a "for-each" instruction.
3
12375
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
2106
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
3629
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
2562
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 various sub types of which I only expect to find ~50 to ~100 matches. Thanks! foreach (baseType...
37
4012
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
2278
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 fine. I'm using the Development Webserver, not IIS. But when I hit "Publish website", it failed...
9
4027
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
12940
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
9605
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10398
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.