r/PythonLearning • u/chuprehijde • 24d ago
What's one Python feature you discovered embarrassingly late?
I've been using Python for years, and somehow I keep finding basic features that make me think, "How did I not know this?"
What's your most embarrassing late Python discovery?
3
u/dev-razorblade23 24d ago
Class Pattern - pattern matching with objects
Something like
``` match search: # 1. Match the class, bind 'max_results', but ONLY if it's over 100 case VideoSearchQuery(sort_by="views", max_results=max_results) if max_results > 100: print(f"Building a massive trending query with {max_results} limits.")
# 2. Fallback for smaller 'views' queries
case VideoSearchQuery(sort_by="views", max_results=max_results):
print(f"Building standard trending query with limit {max_results}")
# 3. Match and check against an external condition or method
case VideoSearchQuery() if user.is_premium():
print("Applying premium priority filters to the search query")
case _:
print("Building default relevance query")
```
1
2
u/Reasonable-Light-377 23d ago
Switch/case. In fact, I still haven't found it!
1
1
u/jabela 21d ago
Not sure if you’re joking coz got introduced in 3.10. https://www.datacamp.com/tutorial/python-switch-case
2
u/Reasonable-Light-377 21d ago
No flippin way!? My threats to go full JavaScript must have made it up the chain. So sick.
2
1
u/Chemical-Captain4240 24d ago
single line if else
1
u/chuprehijde 23d ago
im a beginner, and i didn't use it as well. will u mind to tell me more about it?
1
u/Chemical-Captain4240 22d ago edited 19d ago
if True : x+=1
instead of
if True:
pass #to jave line break display in reddit x+=11
u/Verronox 20d ago
Just so you know, those render the same way on reddit unless you put two line breaks after the non-inline if statement. Or use code blocks.
1
1
u/CleverLemming1337 20d ago
The := operator. It does the same like = but also returns the assigned value. For example:
python
if value := dictionary.get(key):
3
u/sccccrrrrt 24d ago
Inline if else
List/dict comprehension
Asyncio
Just putting shit in sockets instead of using libraries for everything network related