Everything I know about Python...

Learn to Write Pythonic Code!

Check out the book Writing Idiomatic Python!

Discuss Posts With Other Readers at discourse.jeffknupp.com!

A Nice Little Bit of Python

The blog has been relatively quiet recently due to my many irons and fires and all that, but I did want to take a second and post a simple bit of code which really appeals to me. It solves a common problem in a reasonably elegant way and is straightforward enough for Python programmers of any level to use.

The best way to show it is to show an example of the type of problem it solves. Say we have a string and want to make sure it ends with one of four sub-strings. Here is the usual method of checking this:

if needle.endswith('ly') or needle.endswith('ed') or 
    needle.endswith('ing') or needle.endswith('ers'):
    print('Is valid')
else:
    print('Invalid')

Ugly, right? If we were just checking if the string needle was equal to one of those values we could use this common idiom:

if needle in ('ly', 'ed', 'ing', 'ers'):
    print('Is valid')
else:
    print('Invalid')

Alas, we can't use in when a function call like endswith is involved. When we say exactly what we want to check aloud, however, the more elegant solution becomes obvious:

I want to check if the string ends with any of the given sub-strings

The key word in there is any, which happens to be a Python built-in. Instead of the overly clunky method of checking endswith used above, why not take a page from the idiomatic set-membership check:

if any([needle.endswith(e) for e in ('ly', 'ed', 'ing', 'ers')]):
    print('Is valid')
else:
    print('Invalid')

Now, some readers might be disappointed here, having expected a far more earth-shattering revelation. And that's OK. I'm sure a large portion of you already use a method similar to this when faced with the same situation. The interesting bit was that I was able to reason it out given my knowledge of existing idioms.

I didn't read it in someone else's blog post or while reading some library's code, I reasoned it out on my own. Now of course it existed before I discovered it and is old hat for many, but the point is that I discovered it without being shown it. And I was able to do so because of my knowledge of Python idioms.

So that's that. Stay tuned for a number of posts I have in the works (some more controversial than others) in addition to the videos for the Kickstarter campaign. I'm still giving away free books for women in STEM, so email me if that fits your description and you'd like a copy of Writing Idiomatic Python.

Read on →



An Overwhelming Reponse From Women in STEM

My last post announced that I was making available to women in STEM Writing Idiomatic Python and two free tutoring sessions (I have privately tutored individuals in Python for some time). I hoped that the response would be positive. I was not prepared for just how positive it has been.

Read on →


In Support of Female Engineers

My last post, How DevOps is Killing the Developer, received quite a bit of attention on social media and technology news sites. The response was both positive and negative, but the negative reactions were really negative. I've been wondering what I can do to harness the article's popularity and turn that negative energy into something positive.

tl;dr: Female engineers are now entitled to a free copy of Writing Idiomatic Python and two hour-long private tutoring sessions. Further tutoring sessions are discounted.

Read on →


How 'DevOps' is Killing the Developer

There are two recent trends I really hate: DevOps and the notion of the "full-stack" developer. The DevOps movement is so popular that I may as well say I hate the x86 architecture or monolithic kernels. But it's true: I can't stand it. The underlying cause of my pain? This fact: not every company is a start-up, though it appears that every company must act as though they were.

Read on →


Web Analytics