Python Forum
Regex: finding if three groups have a value in them
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex: finding if three groups have a value in them
#7
It compares the object ID which is the memory location of the object.

In earlier versions of Python you could do nasty things like change the value of True and False. Nobody raised a peep if you wrote:
x = True
True = 1
if x != True:
    print x, '!=', True
However if you wrote his code using is
x = True
True = 1
if x is not True:
    print(x, '!=', True)
The output of the first example is indeterminant. Maybe True was set to 1. The second example never prints because no matter what value we assign True, it always "is" True. It is comforting to write code that cannot be broken.

"is" is slightly more efficient. When testing "x == True" we need to call __eq__(x, True which has to get the value in x and the value in True and do a comparison. When using "x is True" we can directly compare the object ID's.

"Comparisons to singletons like None should always be done with is or is not, never the equality operators."

"never" is a bad word. Many small integers are singletons. I don't remember the exact range, some integers are singletons:
x = 1
print('x is 1', x is 1)
x = 1000
print('x is 1000', x is 1000)
Output:
Warning (from warnings module): File "C:\Users\djhys\Documents\Python\Musings\junk.py", line 4 print('x is 1000', x is 1000) SyntaxWarning: "is" with a literal. Did you mean "=="? >>> =========== RESTART: C:\Users\djhys\Documents\Python\Musings\junk.py =========== x is 1 True x is 1000 True
At least they give you a warning.

But the message in that message is a good one. When you can use it, "is" is a better test than "==", and both are bar better than the implicit test in "if x:" If "x" what? If x != 0 and x != None and x != False?
Reply


Messages In This Thread
RE: Regex: finding if three groups have a value in them - by deanhystad - May-12-2020, 09:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Random student selection from groups. esahan 2 99 5 hours ago
Last Post: esahan
  How to group related products in relationship groups? RegionHUser 2 327 Jun-02-2024, 03:51 PM
Last Post: Pedroski55
  Ldap Search for finding user Groups ilknurg 1 1,925 Mar-11-2022, 12:10 PM
Last Post: DeaD_EyE
  Regex not finding all unicode characters tantony 3 2,414 Jul-13-2021, 09:11 PM
Last Post: tantony
  Make Groups with the List Elements quest 2 2,098 Jul-11-2021, 09:58 AM
Last Post: perfringo
  Understanding Regex Groups matt_the_hall 5 3,035 Jan-11-2021, 02:55 PM
Last Post: matt_the_hall
  How to solve equations, with groups of variables and or constraints? ThemePark 0 1,770 Oct-05-2020, 07:22 PM
Last Post: ThemePark
  Create homogeneous groups with Kmeans ? preliator 0 1,638 Sep-01-2020, 02:29 PM
Last Post: preliator
  How to take group of numbers summed in groups of 3... jaguare22 1 1,622 May-05-2020, 05:23 AM
Last Post: Yoriz
  Listing groups tharpa 2 2,708 Nov-26-2019, 07:25 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020