Let's look at some terrible Python code...

Hey there,

Check out this Python snippet newsletter member Cliff sent me (screenshot):

Image

Cliff attended a Python workshop the other day and the instructor there used this as a code example.

I almost spit my breakfast oatmeal across the table when I heard this was used in somebody's class to teach people Python...

Here's why this is a TERRIBLE example for teaching the basics of object-oriented programming in Python:

- class Customer() <-- Why the empty parentheses? Either extend from object on Python 2 or leave them out on Py 3.

- The formatting is way off. What's the deal with all this extra whitespace around assignment operators?

- The random "print" call in there...

- Confusing naming: What's a "customerdet"? A typo for "customer debt" maybe?

I guess it stands for "customer details", but the fact that I had to guess shows how cryptic these method names are...

Also, "cashmethod" anyone?

- And lastly, the elephant in the OOP shop:

Separation of Concerns Violations

You see, this class makes heavy use of the "input()" method to read interactive user input from the command-line.

Let me explain why this is a bad idea:

In my mind, this Customer class shouldn't *care* about whether its data comes from terminal input, or a file, or from the network—

All it needs to know are the actual values for the customer's name, the payment method, and so on.

This is a "model" class that serves as a data store and operations on that particular data and it should NOT concern itself with parsing user input.

(Also note the fact that this class does ZERO input validation or error handling.)

If these different concerns get mixed up we end up with a hot mess that's hard to test:

To write automated tests for this Customer class we'll have to override "input()" or replace it with a mock object.

And as a result, our test cases become more complex and difficult to maintain.

That's a pretty hefty downside.

The fact that someone used this code as an example of good Python code in their "teaching materials" boggles my mind...

There's a lot of demand for Python training right now.

And not everyone who's teaching knows what they're doing.

So if you're going through someone's Python tutorial, book, or course and it doesn't seem to make much sense—it may not be your fault at all.

Happy Pythoning!

— Dan Bader

Older messages

[Last Call] Master Python Fundamentals & Build Real Apps With "Python Basics"

Thursday, October 8, 2020

You're almost out of time though... The 27% off launch sale on our new Python Basics book ends in TODAY: Image Tomorrow the price of the book will go up (no exceptions) With Python Basics you'

What Python developers say about "Python Basics"

Wednesday, October 7, 2020

Get a Step-By-Step Python 3 Curriculum And Save 27% on the "Python Basics" Book Claim Your Discount (Ends Thursday at 11:59pm) Hey there, I'm over the moon that our Python Basics book is

Get Your Complete Python 3 Curriculum (Book Launch)

Tuesday, October 6, 2020

Python Basics (new book) is available at a 27% Launch Discount until Thursday night Click here to learn more and claim your discount Hey there, There's an overwhelming amount of information about

New Python tutorials on Real Python

Saturday, October 3, 2020

Hey there, There's always something going on over at realpython.com as far as Python tutorials go. Here's what you may have missed this past week: The Python return Statement: Usage and Best

artisanal from-scratch programmers

Sunday, September 27, 2020

Hey there, The other day I got this question from Newsletter member Newvick in my email inbox: ~~~ I found your post on books for people looking to move past the beginner's stage in python, and it

You Might Also Like

Import AI 399: 1,000 samples to make a reasoning model; DeepSeek proliferation; Apple's self-driving car simulator

Friday, February 14, 2025

What came before the golem? ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

Defining Your Paranoia Level: Navigating Change Without the Overkill

Friday, February 14, 2025

We've all been there: trying to learn something new, only to find our old habits holding us back. We discussed today how our gut feelings about solving problems can sometimes be our own worst enemy

5 ways AI can help with taxes 🪄

Friday, February 14, 2025

Remotely control an iPhone; 💸 50+ early Presidents' Day deals -- ZDNET ZDNET Tech Today - US February 10, 2025 5 ways AI can help you with your taxes (and what not to use it for) 5 ways AI can help

Recurring Automations + Secret Updates

Friday, February 14, 2025

Smarter automations, better templates, and hidden updates to explore 👀 ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

The First Provable AI-Proof Game: Introducing Butterfly Wings 4

Friday, February 14, 2025

Top Tech Content sent at Noon! Boost Your Article on HackerNoon for $159.99! Read this email in your browser How are you, @newsletterest1? undefined The Market Today #01 Instagram (Meta) 714.52 -0.32%

GCP Newsletter #437

Friday, February 14, 2025

Welcome to issue #437 February 10th, 2025 News BigQuery Cloud Marketplace Official Blog Partners BigQuery datasets now available on Google Cloud Marketplace - Google Cloud Marketplace now offers

Charted | The 1%'s Share of U.S. Wealth Over Time (1989-2024) 💰

Friday, February 14, 2025

Discover how the share of US wealth held by the top 1% has evolved from 1989 to 2024 in this infographic. View Online | Subscribe | Download Our App Download our app to see thousands of new charts from

The Great Social Media Diaspora & Tapestry is here

Friday, February 14, 2025

Apple introduces new app called 'Apple Invites', The Iconfactory launches Tapestry, beyond the traditional portfolio, and more in this week's issue of Creativerly. Creativerly The Great

Daily Coding Problem: Problem #1689 [Medium]

Friday, February 14, 2025

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Google. Given a linked list, sort it in O(n log n) time and constant space. For example,

📧 Stop Conflating CQRS and MediatR

Friday, February 14, 2025

​ Stop Conflating CQRS and MediatR Read on: m​y website / Read time: 4 minutes The .NET Weekly is brought to you by: Step right up to the Generative AI Use Cases Repository! See how MongoDB powers your