473,805 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

conventions/requirements for 'is' vs '==', 'not vs '!=', etc

I'm wondering what is the canonical usage of the keywords 'is' and
'not' when you're writing conditionals and loops. The one I've been
following is completely arbitrary--I use the symbols '==', '!=' for
numerical comparisons and the words 'is', 'not' for everything else.

Thanks in advance!
Jun 27 '08
21 1017
Asun Friere wrote:
Well you have to be careful in case some smartarse comes back with a
class with something like this in it:

def __eq__ (self, other) :
if self is other : return False
That's pretty paranoid. :)
Who said that? (:

Uli

--
Sator Laser GmbH
Geschäftsführ er: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

Jun 27 '08 #11
Gabriel Genellina a écrit :
En Mon, 19 May 2008 17:58:48 -0300, br************* ****@gmail.com
<br************ *****@gmail.com escribió:
>On 19 mai, 22:29, Luis Zarrabeitia <ky...@uh.cuwro te:
(snip)
>>The main
concept here: identity [usually] implies equality,

I really enjoyed the "usually" disclaimer !-)

In some *rare* cases, identity does not imply equality:
Yeps. That's exactly why I enjoyed this disclaimer...

(snip nan example).
Jun 27 '08 #12
John Salerno <jo******@NOSPA Mgmail.comwrote :
>>>a = 'this is longer'
b = 'this is longer'
a == b
True
>>>a is b
False
>>>>

In the above example, Python has created only one string called
'hello' and both x and y reference it. However, 'this is longer' is
two completely different objects.
That is true when run interactively, but the behaviour changes again if you
run it as a script:

C:\Temp>type t.py
a = 'this is longer'
b = 'this is longer'
print a is b

C:\Temp>t
True

In short, two equal strings may or may not be identical and any code which
makes assumptions based on observed behaviour is broken. If you want to be
able to test identity on strings safely then use the 'intern()' builtin to
get repeatable behaviour.

--
Duncan Booth http://kupuguy.blogspot.com
Jun 27 '08 #13
On May 20, 5:04*am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
John Salerno <johnj...@NOSPA Mgmail.comwrote :
>>a = 'this is longer'
b = 'this is longer'
a == b
True
>>a is b
False
In the above example, Python has created only one string called
'hello' and both x and y reference it. However, 'this is longer' is
two completely different objects.

That is true when run interactively, but the behaviour changes again if you
run it as a script:

C:\Temp>type t.py
a = 'this is longer'
b = 'this is longer'
print a is b

C:\Temp>t
True

In short, two equal strings may or may not be identical and any code which
makes assumptions based on observed behaviour is broken. If you want to be
able to test identity on strings safely then use the 'intern()' builtin to
get repeatable behaviour.

--
Duncan Boothhttp://kupuguy.blogspo t.com
Strictly speaking, identity is a real notion whereas equality is an
ideal (symbolic) notion. There is no such thing as real true equals.
Furthermore, there are no true real symbols. But, as puzzling as that
is:

'abc' == 'abc'
abc is abc

I argue that 'is' should never be used for primitives/literals.

a is 4 #bad
a is b #good
a is 'abc' #bad
a is { None: None } #bad
a== { None: None } #good

Just good thing I don't have check-in priveleges, right? :)
Jun 27 '08 #14
On May 20, 9:04 am, castironpi <castiro...@gma il.comwrote:
On May 20, 5:04 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
John Salerno <johnj...@NOSPA Mgmail.comwrote :
>>>a = 'this is longer'
>>>b = 'this is longer'
>>>a == b
True
>>>a is b
False
In the above example, Python has created only one string called
'hello' and both x and y reference it. However, 'this is longer' is
two completely different objects.
That is true when run interactively, but the behaviour changes again if you
run it as a script:
C:\Temp>type t.py
a = 'this is longer'
b = 'this is longer'
print a is b
C:\Temp>t
True
In short, two equal strings may or may not be identical and any code which
makes assumptions based on observed behaviour is broken. If you want to be
able to test identity on strings safely then use the 'intern()' builtin to
get repeatable behaviour.
--
Duncan Boothhttp://kupuguy.blogspo t.com

There is no such thing as real true equals.
That's quite some dark deep philosophy. Fortunately, computers are not
as complicated as real life. I would argue that, programatically ,
there *is* such thing as "real true equals". I would also argue that
that view is most certainly not shared by everyone.
Jun 27 '08 #15
"Duncan Booth" <du**********@i nvalid.invalidw rote in message
news:Xn******** *************** **@127.0.0.1...
That is true when run interactively, but the behaviour changes again if
you
run it as a script:
Yeah, I forgot to mention that you shouldn't rely on this behavior. :)
Jun 27 '08 #16

"Duncan Booth" <du**********@i nvalid.invalidw rote in message
news:Xn******** *************** **@127.0.0.1...
| In short, two equal strings may or may not be identical and any code
which
| makes assumptions based on observed behaviour is broken. If you want to
be
| able to test identity on strings safely then use the 'intern()' builtin
to
| get repeatable behaviour.

Intern() is gone in 3.0

Jun 27 '08 #17
On May 20, 12:09 pm, "Terry Reedy" <tjre...@udel.e duwrote:
Intern() is gone in 3.0
But not gone far:
>>import sys
sys.intern
<built-in function intern>
Jun 27 '08 #18
On May 20, 11:49*am, s0s...@gmail.co m wrote:
On May 20, 9:04 am, castironpi <castiro...@gma il.comwrote:


On May 20, 5:04 am, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
John Salerno <johnj...@NOSPA Mgmail.comwrote :
>>a = 'this is longer'
>>b = 'this is longer'
>>a == b
True
>>a is b
False
In the above example, Python has created only one string called
'hello' and both x and y reference it. However, 'this is longer' is
two completely different objects.
That is true when run interactively, but the behaviour changes again if you
run it as a script:
C:\Temp>type t.py
a = 'this is longer'
b = 'this is longer'
print a is b
C:\Temp>t
True
In short, two equal strings may or may not be identical and any code which
makes assumptions based on observed behaviour is broken. If you want to be
able to test identity on strings safely then use the 'intern()' builtin to
get repeatable behaviour.
--
Duncan Boothhttp://kupuguy.blogspo t.com
There is no such thing as real true equals.

That's quite some dark deep philosophy. Fortunately, computers are not
as complicated as real life. I would argue that, programatically ,
there *is* such thing as "real true equals". I would also argue that
that view is most certainly not shared by everyone.- Hide quoted text -

- Show quoted text -
Mean as it sounds, bits are pigeonholes: 5+V or 0. As such, computers
are able to simulate numerical identity, but, don't forget, all your
inputs are discretized (pigeonholed).

Symbolic (mathematical) identity (equality) is defined as a reflexive,
symmetric, transitive relation, and partitions its domain into
equivalence classes (membership in which is binary). Symbols are
outside of cause.

Ma: Symbolic identity is a mathematical relation
Mb: Symbols are acausal
m: Matter is causal
C: Symbolic identity is not defined on matter.

Further, conclude computers are symbolic only. Now, what can a hoard
of people do with some symbols?

But, I defer on the practical sense.
Jun 27 '08 #19
Lie
On May 20, 10:42*am, John Salerno <johnj...@NOSPA Mgmail.comwrote :
On Mon, 19 May 2008 20:34:22 -0700 (PDT)

notnorweg...@ya hoo.se wrote:
i am confused.
x=5
y=5
x==y -True
x is y -True
shouldnt x is y return False since they shouldnt(dont?) point to the
same place in memory, they just store an equal value?

For some immutable values (such as numbers and strings), Python will have separate variables reference the same object, as an optimization. This doesn't affect anything, since the numbers are immutable anyway and a shared reference won't create any unexpected changes.

It also works with small, but not large, strings:
>x = 'hello'
y = 'hello'
x == y
True
>x is y
True
>a = 'this is longer'
b = 'this is longer'
a == b
True
>a is b
False

In the above example, Python has created only one string called 'hello' and both x and y reference it. However, 'this is longer' is two completely different objects.
But the rule of thumb is: Don't rely on this optimization, it's an
implementation detail.
Jun 27 '08 #20

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

Similar topics

8
2968
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int p3); The parameters here can be read either in the forward order from p1 till p3 or reverse order from p3 till p1. Can anyone explain what is the advantage/disadvantage of either of
7
2487
by: cmiddlebrook | last post by:
Hi there, I keep finding myself getting inconsistent with naming conventions for things like member variables, class names etc and I just want to find something that suits me and stick to it. I am wondering if there are any naming conventions around that are deemed suitable by the general C++ community. I have googled around and I can't find much - mostly long lists of hungarian-like prefixes which is not really what I'm after.
1
6551
by: clintonG | last post by:
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET...
7
2571
by: Ralph Lund | last post by:
Hi. I am starting a new project with C#. I am searching for "good" coding conventions. I know that there are some coding conventions from microsoft, (but they are very extensive and not clear). In the example programs of Microsoft they use different coding conventions: private members sometimes with underscore, sometimes without; when calling a method sometimes: method(param1, param2) or method ( param1, param2) (with or without...
3
4355
by: clintonG | last post by:
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET...
5
6177
by: rastaman | last post by:
Hi all, I know of the existence of Object Naming Conventions for Visual Basic 6. Now I'm reading some books about VB .NET, but the names used for the objects are button1, picturebox1, etc. I wonder if I can use the same naming conventions as for VB6 ? If so, what about the names for the new objects in .NET. Kind regards rastaman
4
5452
by: Patrick | last post by:
what are the general naming conventions for vb.net controls. esp txtUserName, lblPassword, btnSubmit What are the prefixes for the controls???? I've tried to search hi and low on the net, but in vain. I'd much appreciate it if someone could guide me with regards to this. All i need are standards for me to follow by my self. Cuz i'm finding it difficult to make my own darn standards.
18
3289
by: psbasha | last post by:
Hi, I would like to know what naming conventions we can follow for the following types of variables/sequences : Variables : ------------- Integer Float Boolean
10
3269
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a programming system places on how a procedure is called and how data is passed between a calling program and procedures are called calling conventions"
0
9596
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
10604
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...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
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
10103
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
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...
0
6874
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();...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.