Episode 149: Coding With namedtuple & Python's Dynamic Superpowers
The Real Python Podcast
Mar 17, 2023 53m
Have you explored Python’s collections module? Within it, you’ll find a powerful factory function called namedtuple(), which provides multiple enhancements over the standard tuple for writing clearer and cleaner code. This week on the show, Christopher Trudeau is here, bringing another batch of PyCoder’s Weekly articles and projects.
Episode Sponsor:
Christopher discusses his Real Python video course about writing more Pythonic code using namedtuple()
. With namedtuple(), you can create immutable sequence types that allow you to access their values using descriptive field names and dot notation instead of clunky integer indices.
We also discuss metaprogramming and the unique advantages of Python’s dynamism. Christopher shares potential paths for this type of coding from web applications, testing, and AST techniques.
We share several other articles and projects from the Python community, including a news update, the Arrow revolution happening in pandas 2.0, a new PEP for inlined comprehensions, tips and techniques for modern Flask apps, a Python helper tool for building and running a REPL with custom commands, and a project to turn a pandas DataFrame into a Tableau-style UI.
Course Spotlight: Writing Clean, Pythonic Code With namedtuple
In this video course, you’ll learn what Python’s namedtuple
is and how to use it in your code. You’ll also learn about the main differences between named tuples and other data structures, such as dictionaries, data classes, and typed named tuples.
Topics:
- 00:00:00 – Introduction
- 00:02:17 – Python 3.12.0 alpha 6 released
- 00:02:40 – Django Developers Survey 2022 Results
- 00:03:12 – Writing Clean, Pythonic Code With
namedtuple
- 00:07:40 – pandas 2.0 and the Arrow Revolution (Part I)
- 00:17:21 – Sponsor: RevSys
- 00:18:10 – PEP 709: Inlined Comprehensions
- 00:20:51 – 13 Tips and Techniques for Modern Flask Apps
- 00:25:54 – Video Course Spotlight
- 00:27:24 – Discussion: Python’s “Disappointing” Superpowers
- 00:47:54 – replbuilder: Python helper tool for building and running a REPL with custom commands
- 00:49:58 – pygwalker: Turn pandas Into a Tableau-Style UI
- 00:52:15 – Thanks and goodbye
News:
- Python Insider: Python 3.12.0 alpha 6 released
- Django Developers Survey 2022 Results - Django Weblog
Show Links:
- Writing Clean, Pythonic Code With
namedtuple
– In this video course, you’ll learn what Python’snamedtuple
is and how to use it in your code. You’ll also learn about the main differences between named tuples and other data structures, such as dictionaries, data classes, and typed named tuples. - pandas 2.0 and the Arrow Revolution (Part I) – This article details the changes in the pandas 2.0 release, with emphasis on the underlying adoption of Apache Arrow.
- PEP 709: Inlined Comprehensions – Python Enhancement Proposal 709 covers a change to how comprehensions are handled. Currently, they’re compiled as nested functions. Benchmarking shows that treating
list
,dict
, andset
comprehensions as inline code can result in a 2x speedup on the comprehension. - 13 Tips and Techniques for Modern Flask Apps – Flask is approaching its 13th birthday, and to celebrate, Phillip has written 13 tips for writing modern Flask apps. It covers dealing with JSON, environment-based configuration, auto-generated docs, and more.
Discussion:
- Python’s “Disappointing” Superpowers - lukeplant.me.uk
- I am disappointed by dynamic typing - Buttondown
- Python’s “Disappointing” superpowers - Hacker News
- Python’s “Disappointing” Superpowers - Lobsters
Projects:
- replbuilder: Python helper tool for building and running a repl with custom commands
- pygwalker: Turn pandas Into a Tableau-Style UI
Additional Links:
- Write Pythonic and Clean Code With namedtuple – Real Python
- Episode #146: Using NumPy and Linear Algebra for Faster Python Code – The Real Python Podcast
- Apache Arrow and the “10 Things I Hate About pandas” - Wes McKinney
- Data science without borders - Wes McKinney (Two Sigma Investments) - YouTube
- Joining Forces for an Arrow-Native Future - Wes McKinney
- pandas arrays, scalars, and data types — pandas 2.1.0.dev0+171.gc293caf2e9 documentation
- Episode #18: Ten Years of Flask: Conversation With Creator Armin Ronacher – The Real Python Podcast
- Python REST APIs With Flask, Connexion, and SQLAlchemy – Part 1 – Real Python
- Python Metaclasses – Real Python
- Kanaries AI enhanced data exploration