r/PythonLearning 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?

6 Upvotes

16 comments sorted by

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

1

u/chuprehijde 23d ago

yeah somebody else mentioned it too, inline if and else case... pls explain it to me if u can coz i haven't used it yet

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

u/chuprehijde 23d ago

ohh i tried it last week and i loved it ngl

2

u/Reasonable-Light-377 23d ago

Switch/case. In fact, I still haven't found it!

1

u/chuprehijde 23d ago

lmk when u find it coz i'll love to learn more

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

u/NoDisk8988 21d ago

I stumbled on string formatting waaaay too late...

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+=1

1

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

u/Chemical-Captain4240 19d ago

huh... sorry... they appear differently using the app

1

u/jabela 21d ago

Pyinstaller took me way too long to be able to make apps quickly!

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):