how do I make an empty generator? (intermediate) anthony explains

  Рет қаралды 4,729

anthonywritescode

anthonywritescode

Күн бұрын

Пікірлер: 39
@jamesarthurkimbell
@jamesarthurkimbell 3 ай бұрын
Just wait until 3.13 introduces the yieldn't keyword
@ruroruro
@ruroruro 3 ай бұрын
`yield from ()` is indeed pretty elegant. One potential downside of it is that it actually compiles to a bunch of extra bytecode that actually creates the empty tuple and then tries to iterate over it. On the other hand, `return + yield` and `if False: yield` both compile to what seems to be the minimal number of instructions that emulate the required behavior. Also, regarding the correct type signature for such a generator, I think it should be def gen() -> Generator[typing.Never, typing.Any, None]: return yield because the yield type (first position) is covariant, while the send type (second position) is contravariant. So Generator[Never, Any, None] should be the broadest correct annotation. The generator never yields any value, accepts any sent value (because you'll never have an opportunity to actually send anything to it) and return None.
@siddhantdutta4050
@siddhantdutta4050 3 ай бұрын
Was just about to comment this as i was playing around disassembling the components :)
@ccgarciab
@ccgarciab 3 ай бұрын
Showcasing the fix of a bug you found in a widely used library is cool. Empty iterables in general are the modulus of most operations, so they're really important. Thanks for the useful idioms.
@er63438
@er63438 3 ай бұрын
Expectation: to get my mind blown. Reality: got my mind blown.
@alainleufroy
@alainleufroy 2 ай бұрын
Before return was accepted in generator i usually used `while False: yield` 😅
@Jakub1989YTb
@Jakub1989YTb 3 ай бұрын
I'm gonna win so many bets, now that I know "return yield" is a valid syntax.
@Zhaxxy
@Zhaxxy 3 ай бұрын
no, its return yield
@AZIARGROUS
@AZIARGROUS 3 ай бұрын
It’s super fun cause I had to do them yesterday ahahah nice Anthony
@Jakub1989YTb
@Jakub1989YTb 3 ай бұрын
2:44 ... booom ... mind blown
@viddeshk8020
@viddeshk8020 3 ай бұрын
Damm, 😬 The more I learn python the more I am able to understand that I didn't learn anything 😢.
@dandyddz
@dandyddz 2 ай бұрын
Before watching this video fully I was guessing that def a(): if False: yield is the most efficient way of doing that.
@be1tube
@be1tube 3 ай бұрын
My first try was: for i in range(0,0): yield i Works but needs optimization. Worked great.
@redark7
@redark7 3 ай бұрын
I actually had a very similar problem, I'll try the return + yield. In my case, I wanted to have an abstract method that returns an AsyncGenerator, so child classes implement it, as an interface. Well, if you define an async function that returns an AsyncGenerator, most abstract functions only 'pass', which was causing mypy to think the return type was Coroutine[AsyncGenerator] instead. Even with explicit type hinting it did not work, I had to make the abstract function not async for mypy to be happy, but that is even worse in my view...
@shunitsu__
@shunitsu__ 3 ай бұрын
Will you make a 3.13 release highlight video
@anthonywritescode
@anthonywritescode 3 ай бұрын
yep! it just got to rc1 so I'm compiling my thoughts!
@davititchanturia
@davititchanturia 3 ай бұрын
what are the usages of empty generator
@float32
@float32 3 ай бұрын
I’ve always wanted to use a for loop as a spooky noop: for _ in noop(): sys.exit()
@Clepsyd_
@Clepsyd_ 3 ай бұрын
It allows for consistency of the type of object you expect: a generator. I just had a practical use case for this: I had a function that yields elements. Each element is kind of expensive to get, and there was a single, cheap check early in the function that, if True, made the function return `None`. That meant I had to check for `None` first when calling the function. Now by returning an empty generator, I can just do `for e in my_function():` regardless, the type returned is a generator.
@davititchanturia
@davititchanturia 3 ай бұрын
@@Clepsyd_ thank you for answer. Totally Makes sense.
@galenseilis5971
@galenseilis5971 3 ай бұрын
What is an example use case for using an empty generator?
@anthonywritescode
@anthonywritescode 3 ай бұрын
it's right at the beginning!
@ArthurKhazbs
@ArthurKhazbs 2 ай бұрын
@@anthonywritescode I think the question is about a practical example of such usage scenario
@at2704
@at2704 3 ай бұрын
return yeild feels wrong and bad lol
@FunkyELF
@FunkyELF 2 ай бұрын
I paused it at 30 seconds before seeing the answer. The way I'd do it is... def empty_gn(): if False: yield "never" ... or if you want to fool your IDE about unreachable code do this: def empty_gn(): if True in [False]: yield "never" Now I'll watch the rest and see if that's similar to what you do.
@FunkyELF
@FunkyELF 2 ай бұрын
ah... cool. I like "yield from ()" ... more readable. Interesting that we both mentioned issues around unreachable code in the two different solutions. Also, I'd argue that mypy was correct that the yield was unreachable. Granted, my `yield "never"` is also unreachable but the day MyPy or PyCharm start executing code to determine if something is reachable or not is the day I stop using it anyway. My old trick for this with PyCharm was to use "if 1 == 1", then "if 1 + 1 == 2", but PyCharm started evaluating that stuff. I'm not really happy about that.
@AlexLaletin-fr9eh
@AlexLaletin-fr9eh 3 ай бұрын
I really like how we answered the "How" question but skipped the "WTF why?"
@michaelconnell9928
@michaelconnell9928 3 ай бұрын
He answers it like 45 seconds into the video
@Zmunk19
@Zmunk19 3 ай бұрын
in short, if you want to be able to swap in a null-generator somewhere that only accepts generators you'd need this
faster git recloning* (intermediate) anthony explains #578
6:17
anthonywritescode
Рет қаралды 2,7 М.
py-spy saved our python 3.11 rollout (intermediate) anthony explains #568
13:48
Farmer narrowly escapes tiger attack
00:20
CTV News
Рет қаралды 5 МЛН
Noodles Eating Challenge, So Magical! So Much Fun#Funnyfamily #Partygames #Funny
00:33
adding test == others fail??? (intermediate) anthony explains #572
10:40
anthonywritescode
Рет қаралды 4,3 М.
typing the untype-able with mypy plugins (advanced) anthony explains #574
25:06
Senior Devs Use These React Hooks
7:05
Youssef Benlemlih
Рет қаралды 3,3 М.
stop making giant changesets!
14:12
anthonywritescode
Рет қаралды 7 М.
oops I'm the pyuwsgi maintainer now (intermediate) anthony explains #579
22:55
how does python's module `__getattr__` actually work?
17:38
anthonywritescode
Рет қаралды 6 М.
debugging flaky cascading failure after upgrading pytest!
26:15
anthonywritescode
Рет қаралды 3,7 М.
Farmer narrowly escapes tiger attack
00:20
CTV News
Рет қаралды 5 МЛН