What is "hashable" in Python?
Sets and dictionaries are powered by hashability. And hashable objects tend to be immutable.
Note that implementing __eq__ will remove the hashability of your class instances by default, which is PROBABLY fine.
If you're making immutable objects that need to be valid dictionary keys or set items, you probably don't need to care about hashability.
pym.dev/what-are-has...
07.03.2026 21:44
π 4
π 0
π¬ 0
π 0
Overloading equality in Python
Want to customize what "equality" means for your class instances in Python? Implement a __eq__ method! Make sure to return NotImplemented as appropriate though.
After __init__ and __repr__, the next dunder method to consider implementing is __eq__.
The __eq__ method controls what it means for your object to be "equal" to another object (with ==).
pym.dev/overloading-...
07.03.2026 21:44
π 3
π 0
π¬ 1
π 0
Python Tip #66 (of 365):
Consider implementing __eq__ on every class you make π§΅
Tips 63 and 64 were about always implementing __init__ and __repr__ methods on your classes.
You don't ALWAYS need a __eq__ method, but you should seriously consider one.
#Python #DailyPythonTip
07.03.2026 21:44
π 4
π 0
π¬ 1
π 0
The people standing behind a booth with lots of swag for PyCon US SoCal Python, and PyBeach.
The Python Community Megabooth is just about ready at #scale23x. We're here with π @pycon.us andπ Socal Python at booth 235, come by and say hi!
06.03.2026 21:35
π 10
π 6
π¬ 0
π 0
But most objects don't implement __str__.
It MIGHT make sense for your class instances to have 2 different string representations, but most classes don't.
It's okay to leave __str__ unimplemented.
But ALWAYS implement __repr__ (see tip #64).
06.03.2026 23:45
π 3
π 0
π¬ 0
π 0
Python's pathlib.Path class implements 2 different string representations:
>>> import pathlib
>>> p = pathlib.Path()
>>> repr(p)
"PosixPath('.')"
>>> str(p)
'.'
So does datetime.βdate:
>>> from datetime import date
>>> d = date(1999, 12, 31)
>>> repr(d)
'date(1999, 12, 31)'
>>> str(d)
'1999-12-31'
06.03.2026 23:45
π 3
π 0
π¬ 1
π 0
Classes that don't implement __str__ will end up with the str representation falling back to repr.
Lists, tuples, and most Python objects only implement __repr__, so their 2 string representations are equivalent:
>>> p = (1, 2)
>>> repr(p)
'(1, 2)'
>>> str(p)
'(1, 2)'
06.03.2026 23:45
π 3
π 0
π¬ 1
π 0
Python Tip #65 (of 365):
Rarely define a __str__ method π§΅
A __str__ method controls the human-readable string representation of your class instances.
You may think you need __str__, but most classes really don't need this method and your class probably doesn't either.
#Python #DailyPythonTip
06.03.2026 23:45
π 2
π 0
π¬ 1
π 0
There is one exception to this rule: if you're making an object that has no sensible state to display, you could leave __repr__ undefined.
Python tends to take this approach with its many iterator objects:
>>> enumerate([])
<enumerate object at 0x76dd66978fe0>
05.03.2026 23:45
π 3
π 0
π¬ 0
π 0
Python Tip #64 (of 365):
Ensure your classes all have a sensible __repr__ method π§΅
A __repr__ method controls the default string representation of your class instances.
Pretty much every object should have a nice string representation.
#Python #DailyPythonTip
05.03.2026 23:45
π 4
π 0
π¬ 1
π 0
What is __init__ in Python?
The __init__ method is used to initialize a class. The initializer method accepts self (the class instance) along with any arguments the class accepts and then performs initialization steps.
If your class doesn't have an initializer method and it doesn't inherit from another class, seriously consider whether you need a class.
Classes maintain state and state needs initialization. Also see the related tip #62 on avoiding conditionally present attributes.
pym.dev/what-is-init/
04.03.2026 23:45
π 3
π 0
π¬ 0
π 0
For every class you make, either:
1. create a __init__ method
2. use a tool (like the dataclass decorator) to create a __init__ method
3. inherit from a class that has a useful __init__ method method.
04.03.2026 23:45
π 4
π 0
π¬ 1
π 0
Python Tip #63 (of 365):
Every class should have an initializer π§΅
If your class doesn't have an initializer method, it could probably be a module instead of a class.
#Python #DailyPythonTip
04.03.2026 23:45
π 4
π 0
π¬ 1
π 0
Invent your own comprehensions in Python
Python doesn't have tuple, frozenset, or Counter comprehensions, but you can invent your own by passing a generator expression to any iterable-accepting callable.
Generator expressions can be used as an "invent your own comprehension" tool.
They don't *actually* invent a custom comprehension, but they can be used for effectively that purpose.
I just published a new article on this idea:
pym.dev/custom-compr...
04.03.2026 15:29
π 1
π 0
π¬ 0
π 0
I rarely remember my dreams, but sast night I dreamed that I went to a baseball game with Mark Evan Jackson and someone named Glenn (not Jason Siegel).
04.03.2026 14:46
π 0
π 0
π¬ 0
π 0
You may SOMETIMES see conditionally present attributes (like __wrapped__ on functions that have been decorated) but it's unusual.
Avoid creating conditionally present attributes.
If an attribute might not always be needed, give it a default state that sensibly represents emptiness (like None).
03.03.2026 23:13
π 3
π 0
π¬ 0
π 0
If your class creates objects that might have a specific attribute but also might NOT have that attribute, you'll need to use hasattr to check for the presence of the attribute and/or getattr to look up the attribute.
That can make for confusing code.
03.03.2026 23:13
π 3
π 0
π¬ 1
π 0
Deleting an attribute or a variable name is an odd thing to do.
Creating an object that sometimes has a specific attribute and sometimes doesn't is ALSO odd.
03.03.2026 23:13
π 4
π 0
π¬ 1
π 0
Python Tip #62 (of 365):
Avoid conditionally present attributes π§΅
Python's "del" statement is pretty much ONLY used to delete dictionary keys (or list indexes).
You'll probably never see "del" used to delete an attribute. There's a reason for that.
#Python #DailyPythonTip
03.03.2026 23:13
π 3
π 0
π¬ 1
π 0
"So we should be careful when defining our default argument value, that we don't call functions that could return different values depending on when they're called."
Read more π https://trey.io/rbi1o4
#Python
03.03.2026 05:07
π 3
π 0
π¬ 0
π 0
When are classes used in Python?
While you don't often need to make your own classes in Python, they can sometimes make your code reusable and easier to read.
If you find yourself passing the same object or group of objects into many different functions, consider whether it might be time to create a class & methods.
Classes can certainly be overused, especially by folks moving from very class-oriented languages like Java.
But they can ALSO be underused.
03.03.2026 03:15
π 1
π 0
π¬ 0
π 0
I find this more readable:
server = IMAPChecker(host)
server.authenticate(name, password)
messages = [
server.get_message(u)
for u in server.get_message_uids()
]
server.quit()
It's clear that those method calls are meant to operate on the server object. Not so with the function calls.
03.03.2026 03:15
π 1
π 0
π¬ 1
π 0
Python Tip #61 (of 365):
When passing the same object(s) to different functions, consider a class. π§΅
Consider this code:
server = get_connection(host, name, password)
messages = [
get_message(server, u)
for u in get_message_uids(server)
]
close_connection(server)
#Python #DailyPythonTip
03.03.2026 03:15
π 6
π 0
π¬ 1
π 0
When to use a class in Python
YouTube video by Python Morsels
New screencast on when it makes sense to use a class in #Python
02.03.2026 23:05
π 2
π 0
π¬ 0
π 0
As far as I can tell, uv doesn't have a command to automatically upgrade a pinned version of a package to the latest version.
What do you do to work around this issue?
#Python #uv
02.03.2026 22:26
π 3
π 1
π¬ 2
π 0
Using "else" in a comprehension
While list comprehensions in Python don't support the else keyword directly, conditional expressions can be embedded within list comprehension.
But if you do use an inline "if" expression in a comprehension, at least wrap the inline "if" in parentheses:
sanitized = [
(n if n > 0 else 0)
for n in counts
]
I find that MUCH more readable!
Comprehensions don't have "else", but an inline "if" can make them look like they do.
01.03.2026 19:04
π 3
π 0
π¬ 0
π 0
I find this a bit more readable:
sanitized = [
n if n > 0 else 0
for n in counts
]
But that could still be misunderstood.
I'd recommend avoiding "if" expressions in comprehensions whenever there might be a simpler way to write your code.
01.03.2026 19:04
π 3
π 0
π¬ 2
π 0
Python Tip #60 (of 365):
Be cautious of "if" expressions in comprehensions π§΅
An "if" expression in a "for" loop can look confusing:
sanitized = [n if n > 0 else 0 for n in counts]
That "else" isn't part of the comprehension. It's just inline "if" expression.
#Python #DailyPythonTip
01.03.2026 19:04
π 3
π 0
π¬ 1
π 0
My Biggest AI Fear
YouTube video by Hank Green
This is one of the most thoughtful videos on AI concerns I've seen.
@hankgreen.bsky.social lays out his concerns in 30 minutes and then interviews @author-calnewport.bsky.social for an hour.
01.03.2026 05:48
π 3
π 2
π¬ 0
π 0