Trey Hunner (@treyhunner) 's Twitter Profile
Trey Hunner

@treyhunner

Helping make #Python ah-ha moments🐍πŸͺ

I share weekly Python tips through my newsletter πŸ’Œ trey.io/news

mastodon.social/@treyhunner 🐘
he/him

ID: 2326251343

linkhttps://pym.dev calendar_today04-02-2014 00:43:49

14,14K Tweet

22,22K Followers

439 Following

Trey Hunner (@treyhunner) 's Twitter Profile Photo

Python's slicing syntax only works on sequences, but reversed() works on any reversible iterable! Read more πŸ‘‰ trey.io/ur7l4g #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

How can you improve the readability of long Boolean expressions in Python? Read the full article: Refactoring long boolean expressions β–Έ trey.io/f98u5c #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

Dictionaries maintain the order of their items in insertion order, and you can loop over them in reverse by using the reversed() function, even though they're not sequences. Read more πŸ‘‰ trey.io/ur7l4g #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

Every character in a Python string represents that literal character, with the exception of escape sequences. Read the full article: Newlines and escape sequences in Python β–Έ trey.io/ca0qyn #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

The rule with De Morgan's Law is that you can distribute the negation of a Boolean operation into each sub-expression, as long as you also flip all the Boolean operators. Read more πŸ‘‰ trey.io/f98u5c #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

I find reversed() a lot more readable than Python's slicing syntax, but it's also more flexible. Read more πŸ‘‰ trey.io/ur7l4g #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

Technically \n is called a line feed, though we usually just call it a newline character. Read the full article: Newlines and escape sequences in Python β–Έ trey.io/ca0qyn #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

Define dunder methods as needed in your #Python classes. But don't call dunder methods...unless you really need to. pym.dev/avoid-dunder-m…

Trey Hunner (@treyhunner) 's Twitter Profile Photo

Whenever Python writes a line feed character (\n) to a text file on Windows, that line feed will automatically be converted to a carriage return (\r) followed by a line feed (\n) Read more πŸ‘‰ trey.io/ca0qyn #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

Python's break statement is often used when you need an early break condition, that is a condition which would indicate that you don't need to loop any further because you've found what you're looking for. Read more πŸ‘‰ trey.io/xtql6h #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

We should use the len function to get an object's length, not the __len__ method Read more πŸ‘‰ trey.io/p0h32w #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

I launched a course on decorators a few weeks ago, just for Python Morsels subscribers. It includes over a dozen short videos, each followed by a brief quiz question and an exercise to apply what you've learned. The first couple sections are free. pythonmorsels.com/courses/decora…

Trey Hunner (@treyhunner) 's Twitter Profile Photo

New video on nested functions in #Python This is a feature that's used much more often in JS, though there are certainly some crucial use cases in Python. For example, most function decorators are implemented with a function nested within a function. pym.dev/nested-functio…

Trey Hunner (@treyhunner) 's Twitter Profile Photo

What if you need to compare two iterables while ignoring the order of their items? Read the full article: Checking whether iterables are equal in Python β–Έ trey.io/0frqkp #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

The next time you find yourself using a break statement in a for loop ask: "what is the purpose of this break" and "is there a looping helper in Python that could help me accomplish this?" Read more πŸ‘‰ trey.io/xtql6h #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

FYI: I share a Python tip via email each Wednesday. πŸ’Œ July's tips were about: β€’ When functions are better than classes β€’ How I use the Python docs β€’ Why I use trailing commas β€’ Why I avoid calling dunder methods β€’ The importance of branch coverage pym.dev/newsletter

Trey Hunner (@treyhunner) 's Twitter Profile Photo

Dunder methods are low-level implementation details that the Python interpreter uses to power various Python features. Read the full article: Don't call dunder methods β–Έ trey.io/p0h32w #Python

Trey Hunner (@treyhunner) 's Twitter Profile Photo

Want to get #Python help from me? Or want to bounce an idea off of me? For the next 13 weeks, I'll be hosting a (completely free) weekly office hour. Sign up for free here: pym.dev/office-hours