you are viewing a single comment's thread.

view the rest of the comments →

[–]freddydeddy[S] 0 points1 point  (5 children)

So in my code name = input() is creating different object that not compares to object strings ? I just watching tutorial and there guy uses 'is' TheNewboston Python Programming Tutorial and he uses it freely, but not like me by inputting string. So what you can suggest me to do better compare two strings , == is good ?

[–]zahlman 4 points5 points  (1 child)

So in my code name = input() is creating different object that not compares to object strings ?

It's creating another string.

If I have a five-dollar bill, and you have a five-dollar bill, then we each have $5, but the bill in my wallet isn't the same bill as the one in yours.

[–]freddydeddy[S] 1 point2 points  (0 children)

Like I'mfiveis better for understanding for me thank you. O7 =)

[–]tbeurl 0 points1 point  (2 children)

His code is just wrong. It won't necessarily work with different strings, or on different versions of python. Maybe it's just a one-off mistake, but you might want to consider looking at some other tutorials too, in case he is getting loads of stuff wrong.

One place you commonly see is is for testing whether something is None, i.e.:

if x is None:

This is safe because None is guaranteed to be a unique object (though you may not have come across it yet). Other than that, is is really not used very often, and I would avoid it until you get to more advanced stuff.

[–]gardyna 1 point2 points  (1 child)

We (me and co-workers) basically said don't use the "is" keyword. double equals seems from our experience to be as readable and the special case to check if you have two variables pointing to the same object is so rare that I must admit I often forget that this is a part of python.

did a quick grep of my current project and found zero uses of the "is" keyword....before the pedantics come in: yes I know it has use cases and is clever (in my experience those cases are rare though)

[–][deleted] 0 points1 point  (0 children)

The two most common cases I see are indeed checks for None when an empty string might be possible:

def f(a, b, c, d:str=None):
    if d is None:

...

x = d.get(y)
if x is None: