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

Daily Coding Problem: Problem #1622 [Hard]

Thursday, November 28, 2024

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Amazon. Given an integer k and a string s, find the length of the longest substring that

Mapped | The Average Credit Card Debt in Every U.S. State 💳

Thursday, November 28, 2024

Households in richer states have higher average credit card debt but higher incomes allow them to pay it off faster as well. View Online | Subscribe | Download Our App Presented by: Defiance ETFs

Issue 341 - Free supercharging and FSD offer extended for new Teslas

Thursday, November 28, 2024

View this email in your browser If you are just now finding out about Tesletter, you can subscribe here! If you already know Tesletter and want to support us, check out our Patreon page Issue 341 -

Programmer Weekly - Issue 233

Thursday, November 28, 2024

View this email in your browser Programmer Weekly Welcome to issue 233 of Programmer Weekly. Let's get straight to the links this week. Quote of the Week "All programmers are playwrights and

Python Weekly - Issue 678

Thursday, November 28, 2024

View this email in your browser Python Weekly Welcome to issue 678 of Python Weekly. Let's get straight to the links this week. Articles, Tutorials and Talks Let's build a AI Photo Generator

Web Tools #593 - Frameworks, React Tools, Uncats

Thursday, November 28, 2024

WEB VERSION Issue #593 • November 28, 2024 Advertisement Power Your Web Projects with Cloudways This BFCM As a developer, designer, or tech professional, you need reliable hosting that adapts to your

Python Weekly - Issue 678

Thursday, November 28, 2024

View this email in your browser Python Weekly Welcome to issue 678 of Python Weekly. Let's get straight to the links this week. Articles, Tutorials and Talks Let's build a AI Photo Generator

Introducing the Revamped HackerNoon AI Image Gallery

Thursday, November 28, 2024

Top Tech Content sent at Noon! Advertise on this Newsletter! 50% off!! Read this email in your browser How are you, @newsletterest1? 🪐 What's happening in tech today, November 28, 2024? The

Black Friday Giveaway @ Real Python

Thursday, November 28, 2024

Hey there, Just wanted to let you know that we're doing a special giveaway for Black Friday this week at Real Python 🎁🎉 You can get Python education resources worth over $1210, including three

Dispatch 019 🦃

Thursday, November 28, 2024

Watching the Parade • Paying for the Parade • Pirate TV Raid • US v. Microsoft Round 2 • Trump's Anti-Antitrust? • Google Anti-Antitrust The Spyglass Dispatch is a free newsletter sent out daily on