5 Uncommon Python Features I Love

preview_player
Показать описание
Here are 5 great & uncommon Python features that you can use in your Python projects.

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

The "pipeline" on sets is not a pipeline, but the OR operator (returning a set containing all elements which are in either A or B).

ubtdzot
Автор

00:13 Using slice object
02:03 Set operators
03:51 Custom object string format
07:52 Walrus operator :=
11:51 Currying

rexygama
Автор

Important: when using dict.get, remember to check against is None, rather than an implicit truth check. Empty strings are considered falsy, which may lead to unexpected behaviour. These are 2 extra words that will help you not write bugs

knut-olaihelgesen
Автор

I've seen dozens of "python tips" videos like this and learned almost nothing. They went from giving tips everybody knows to outright horrendously bad code. This one is the first where I actually learned something (that you can pass functions as first class objects) and every tip was actually relatively unknown, but still useful and presented in well written code. Compliment!

anonymousanon
Автор

the custom __format__ can be useful when you have sort of vector class and want to print its value with standard spec. like with f"{vec:.1f}" you get "(1.5, 3.0, 4.7)" and with f"{vec:.2f}" you get "(1.50, 3.01, 4.68)"

ujmqvqd
Автор

I would use partial for simple currying, but function returning functions for more complex or customised currying.

TheJaguar
Автор

4:49 Please be warned that "match" was only introduced in Python 3.10 which is relatively recent

On another note, I personally use this for only one purpose, which is to allow debug prints, it looks like this:
print(f"{my_class:d1}")
d1 is basically "debug-1" which essentially means "print this object with little information". The implementation looks something like this:


class Example:
def __init__(self, field1: str, field2: str, field3: str, field4: str) -> None:
self.field1: str = field1
self.field2: str = field2
self.field3: str = field3
self.field4: str = field4

def __format__(self, __format_spec: str) -> str:
if
level = int(__format_spec[1:])

fields = ["field1"]

if level > 1:
fields.append("field2")
if level > 2:
fields.append("field3")
if level > 3:
fields.append("field4")

fields_dict = {
field_name: self.__dict__[field_name] for field_name in fields
}
return f"{self.__class__.__name__} {fields_dict}"

The actual implementation I use is a bit different, to give better error messages, and to override __str__ to use this (so you can do print(example) directly) but you get the idea...

carlosmspk
Автор

1. when doing a[<smth>], you can extract <smth> as a variable of type "slice".
for example a[slice(None, None, 1)] is the same as a[::-1]
2. bit operations & | work with sets too
3. f-strings can be formated, __format__ can be implemented on types
4. You can use the walrus operator (a:=b) for its intended purpose, which is using it in if statements
4. curry is possible in python

norude
Автор

This is your best video yet IMHO. I didn't know 100% of any of the topics you covered, and while I knew about all of the set operations, I'd no idea you could do the same thing with operators... very cool. Great job! Keep it up!

URNFUND
Автор

This is a totally fine "python features I love" video, but i have one recommendation for the young folks out there. Try not to use the build in set operators as recommend in the video. Stay with the methods which are way more explicit in what they are doing compared to the single characters of the build in operators. They can hide some nasty bugs when not 100% tested.

draufunddran
Автор

I've been using Python for almost 5 years now and im still learning something new everyday from your channel! I love your explanation style ❤

ariqahmer
Автор

The final example, the return type should be Callable[[float], float], to be more specific.

I'm not sure if the ide will infer the contents of [...] there, but if it doesn't then you either should specify that, or not specify a return type.

shubhambhattacharjee
Автор

Great video! The slice trick will be helpful, and I didn't know about that use of the walrus operator you showed inside the dictionary. The walrus operator can also be handy inside of comprehensions if you need to compute something with each element in an iterable, and you want to keep the computational results rather than the original items.

swolekhine
Автор

Whenever programming in numpy (or it's children) the combination of slice objects and boolean index masks are so powerful. You can make the hardest part of convolutional neural networks (from scratch) as legible as a standard loop and multiply pattern.

timedebtor
Автор

Great explanation of the walrus operator. I've struggled to understand it for a while, but I think I've got a good grasp now! Very useful trick.

nephxio
Автор

This was amazingly well explained and useful! Thank you!

imaginaryangle
Автор

Good tips! I used currying to create a text-based prefix notation calculator interface where the parser didn’t need to worry or know about function arity because all functions have an arity of one.

TheWyrdSmythe
Автор

7:34 if, instead of `Any` you could set `format_spec` to be an enum, an array, or a union type, `__format__` would be more useful, and would not require documentation.
At least for the IDE, if not the runtime, it would autocomplete.

I don't know python enough. Maybe this is already possible, but as you didn't mention this option, I assume it can't.

misamee
Автор

Set operations also work with inplace operators like |= and -=. In newer Python versions "|" also works with dicts.
Walrus operator is great for regex matching i.e. "if match := re.match(...)".
Currying is super useful for giving parameters to decorators. Also Callable should be used with parameters to give a more accurate type.

Soul-Burn
Автор

Thanks for sharing, the set portion is very valuable.

RuneJohannesen
welcome to shbcf.ru