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

Maybe Two Big Research Breakthroughs or Maybe Nothing

Sunday, May 5, 2024

Multi-token prediction and a multi-layer perceptron alternative. ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

Dot Leap 2024-8: Paragraph + DGCM = decentralized newsletter?

Sunday, May 5, 2024

Thank you for taking the Dot Leap!We cover Polkadot, Kusama, Polkadot-SDK, and all related Web 3.0 projects! Want your content featured? X ... ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

Card Buddy/Bread Book/Pocket Translator

Sunday, May 5, 2024

Recomendo - issue #409 ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

Kotlin Weekly #405

Sunday, May 5, 2024

ISSUE #405 5th of May 2024 Hello Kotliners! We're already in May and getting closer to KotlinConf. Any predictions for the keynote announcements? Check out our selection of links for the upcoming

📈 Why Is My Ping So High While Gaming? — How to Keep Your Android From Overheating

Saturday, May 4, 2024

Also: Using ChatGPT to Craft a Resume, and More! How-To Geek Logo May 4, 2024 📩 Get expert reviews, the hottest deals, how-to's, breaking news, and more delivered directly to your inbox by

JSK Daily for May 4, 2024

Saturday, May 4, 2024

JSK Daily for May 4, 2024 View this email in your browser A community curated daily e-mail of JavaScript news The Power of React's Virtual DOM: A Comprehensive Explanation Modern JavaScript

Daily Coding Problem: Problem #1431 [Medium]

Saturday, May 4, 2024

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by MongoDB. Given a list of elements, find the majority element, which appears more than

Ranked | The World's Top Media Franchises by All-Time Revenue 📊

Saturday, May 4, 2024

From Pokémon to Star Wars, some media franchises are globally recognizable. How do media franchises compare in terms of all-time revenue? View Online | Subscribe Presented by Voronoi: The App Where

Noonification: Read Code Like a Hacker With the SAST

Saturday, May 4, 2024

Top Tech Content sent at Noon! Get Algolia: AI Search that understands How are you, @newsletterest1? 🪐 What's happening in tech today, May 4, 2024? The HackerNoon Newsletter brings the HackerNoon

Weekend Reading — May the fourth

Saturday, May 4, 2024

This week we setup our new Minecraft server, play Spacewar, avoid burnout, wonder about Facebook AI spam, lose our passkeys, and claim stairs on the way back home. 😎 Labnotes (by Assaf Arkin) Weekend