DEV Community

Cover image for 3 Tricky Python Nuances

3 Tricky Python Nuances

jess unrein on January 16, 2019

This blog post is adapted from a talk I gave last week at ChiPy, the Chicago Python User Group. For more great content like this, I highly recommen...
Collapse
 
mexchip profile image
José Jorge

Good info!

I got caught by number 2 in my previous project, I was trying something like this:

def some_function(some_date = datetime.utcnow()):
    ...
Enter fullscreen mode Exit fullscreen mode

I'd get the same date everytime some_function was called.

Changed to something like this:

def some_function(some_date = None):
    if some_date is None:
        some_date = datetime.utcnow()

    ...
Enter fullscreen mode Exit fullscreen mode
Collapse
 
codemouse92 profile image
Jason C. McDonald

Oof, that's a keeper!

Collapse
 
nviard profile image
Nicolas Viard

1) you should use a linter like pylint to avoid mistakes like this
2) is can be confusing for small integers because they share references
1 is 1 = True
158896587456 is 158896587456 = False

Collapse
 
thejessleigh profile image
jess unrein • Edited

1.) I mean. I almost always find “you should use a linter and then you can avoid learning” to be an unhelpful piece of advice. Also, a linter’s not going to tell you why a nested array is behaving unexpectedly.
2) And true. Python can definitely get a little weird around the edges when you get to arbitrarily large numbers. I hope you’re not hardcoding and checking for identity for integers like this in your code.

You clearly understand references very well! These short examples are, of course, aimed at introducing a concept and encouraging people to learn more. Thanks for providing some trivia!

Collapse
 
modaf profile image
Modaf

I tried 158896587456 is 158896587456 on Python 3.6.1 and it answers True :/
(Even with huge numbers it does answer True)

Collapse
 
nviard profile image
Nicolas Viard

Sorry, this is true when (at least) one of them is stored into a variable:

>>> a = 1
>>> b = 1
>>> print(a is b)
True
>>> a = 123456789
>>> b = 123456789
>>> print(a is b)
False
Thread Thread
 
modaf profile image
Modaf

Indeed ! But it's weird that 123456789 is 123456789 = True, isn't it ? As if Python answers True for a is b if "a is the same as b" before testing if they are the same. Is there a reason for that ?

(123456789+0) is 123456789 = False
but 123456789 is 123456789 = True

Anyway thanks for the example and the answer !

Thread Thread
 
veky profile image
Veky • Edited

Optimizer gets better and better. In 3.7, even 123456788+1 is 123456789 gives True. It's simply that Python realizes it can evaluate constant pure expressions before compiling to bytecode. And it folds constants, of course.

>>> compile('123456788+1 is 123456789', '', 'eval').co_consts

You'll notice there's only one 123456789 there, not two. And there are no 123456788 nor 1.

Collapse
 
ramalhoorg profile image
Luciano Ramalho ☔ 🐍 ⒼⓄ

Good points about lists and default argument. I disagree with your take on is v ==. It is an anti-pattern to test booleans with is or ==. They are already booleans, so there is no reason to write if x is True:, just write if x:.

Also, Python is happy to use any value in boolean contexts, which makes it even less common to handle booleans explicitly as you suggest.

The only really common case of using is in Python code is to test for None, as in if x is None:. This is clear, explicit, and performant, because None is singleton, and there is no overloading for the is operator, so it is faster than == which can be overloaded via the __eq__ special method. Other than that, uses cases that actually require or recommend the use of the is operator are extremely rare.

Collapse
 
thejessleigh profile image
jess unrein

I have worked in a couple teams where my engineering managers had a real stick in their craw about always using is comparators with booleans to the point of rejecting PRs that didn't follow this pattern. But you may be right that the more Pythonic way would be to write if x: when checking for Truth. It's a pattern that I've had drilled into my head that might, in fact, not be as idiomatically Pythonic as I had been led to believe.

However, to your point, if you want to check that something is explicitly False (as opposed to something being False or None) it makes sense to me to use an explicit comparator rather than relying on if not x: as if not reads None as a falsey value. Granted, the use case here is pretty narrow, so it's not super useful.

Thank you for the clarification!

Collapse
 
kaelscion profile image
kaelscion

My favorite quote I've ever heard about Python is this: "Just like Stephen Wright, Python's greatest strength is how well it does one-liners" and I shamelessly use it all the time in posts and comments 😁. List, dict, set, and generator comprehensions are great strengths even when compared with lambdas or zip() functions. Great article as always Jess!😎😎

Collapse
 
djangotricks profile image
Aidas Bendoraitis

Great article. Makes sense.

However, it looks like you posted the list_append() examples without testing them beforehand, for example, these are equivalents:

print(list_append(5, 7))
print(list_append(element=5, input_list=7))

and you cannot call .extend() on an integer.

Collapse
 
thejessleigh profile image
jess unrein • Edited

Oops, this is my bad retyping something from a screenshot of a slide, so I couldn't copy paste. Thanks for the catch! It should read print(list_append([5, 7])), as the example below it does, and I've amended the text to reflect this.

Collapse
 
gadse profile image
Peter

Wow, thanks for the article and your concise explanations! :) I didn't know the first nuance before. And it's by sheer luck that it didn't hurt my work already, since I used the * notation for lists before - luckily without changing any of the referenced objects afterwards.

And I nearly forgot about the second nuance (shame on me).

Collapse
 
tvanantwerp profile image
Tom VanAntwerp

I'm certain #2 (and maybe in concert with #1) cost me hours of my life before I learned what was happening. Great list of gotchas!

Collapse
 
modaf profile image
Modaf

Have learned something, ty

Collapse
 
micuffaro profile image
Michael

Thanks for the great article!

Collapse
 
codemouse92 profile image
Jason C. McDonald

Great insight. I cross-linked to this at the end of Dead Simple Python: Data Types and Immutability.

Collapse
 
codemouse92 profile image
Jason C. McDonald

Read this again, at the same time as watching Ned Batchelder's legendary "Facts and Myths About Python Names and Values", and this article is pretty well spot on.

Collapse
 
thejessleigh profile image
jess unrein

I haven’t seen that one. I’ll have to check it out!