Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Using ‘Yield’ in Python — An Insight into Generators

Sign upSign InSign upSign InMember-only storyArslan MirzaFollowLevel Up Coding--ListenShare“Hey folks, ever wanted to invite a magician into your Python code? Someone who could conjure a torrent of data out of thin air? Well, meet ‘yield’, Python’s resident magician!”In this article, we’ll embark on a thrilling adventure through the realms of Python. As we saunter into the mystical land of ‘yield’ and ‘generators’, you will unravel the secrets to crafting more efficient and more magical Python code.So, grab your spellbooks (read: Python IDEs) and your wands (read: your wits), and let’s cast some code!In Python, the spell ‘yield’ is used in the heart of a function like a return statement, but with a delightful twist! Instead of ending the function and returning the result, ‘yield’ pauses the function and saves its state for resuming later.It’s like pausing a video game mid-battle to fetch a snack, then resuming right where you left off, snack in hand, ready to tackle the boss again.“Wait, what’s that? This looks like a function, but where’s the ‘return’? It’s ‘yield’ing, not returning!” Don’t worry, this isn’t a Python existential crisis; this is our first encounter with a generator!A Python wizard (developer) needs to understand that generators are not your ordinary incantations (functions). Instead, they’re more like those fancy, self-spelling wands from high-end wand stores. Instead of creating a list and storing it in memory, generators ‘generate’ values on the fly.This feature helps Python wizards save precious memory, and is especially handy when dealing with larger data streams.Generators, with their memory-preserving trick, help us handle massive amounts of data without straining our Python-powered magic brooms (computers).“Battle commence! Generators vs Lists! Who will win the crown of efficiency?” With generators, you don’t need to store a colossal list in memory. They lazily produce the items on the fly, making your programs more memory-friendly.The list-based code, on the other hand, demands a massive chunk of memory for storing the entire list. That’s like trying to fit a dragon into a rabbit hole!In Python 3.3, the ‘yield from’ spell was introduced, allowing a generator to yield values from a sub-generator.“Did you see that? ‘yield from’? That’s like saying, ‘Let’s cast the spell from another spell book’”. It’s Python’s way of making your code cleaner and more readable.This is also a part of Python’s commitment to ensuring the code is as easy on the eyes as a unicorn galloping across a rainbow.Just when you thought Python couldn’t get more magical, Python 3.6 introduced asynchronous generator functions and asynchronous generator expressions, taking ‘yield’ to another level.In this realm of asynchronous magic, ‘yield’ pauses function execution, allowing other tasks to run in the meantime. It’s like saying, “Hold my magic staff, Python. I need to feed my pet Griffin, but I’ll be right back to finish this spell.”Keep in mind, with the magical power of ‘yield’, comes great responsibility. Generators and ‘yield’ can make your Python code more efficient, and with a bit of practice, you’ll be casting Python spells like a seasoned wizard in no time.So, fellow wizards, are you ready to yield the power of ‘yield’ and generators to conjure up some Pythonic magic?levelup.gitconnected.comlevelup.gitconnected.comlevelup.gitconnected.com⚡ Support my writing by joining Mediumarslanmirza.medium.comThanks for being a part of our community! Before you go:🚀👉 Join the Level Up talent collective and find an amazing job----Level Up CodingWriter | +500K views on Medium | ⚡ Support my writing by joining Medium https://arslanmirza.medium.com/membershipHelpStatusWritersBlogCareersPrivacyTermsAboutText to speechTeams



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Using ‘Yield’ in Python — An Insight into Generators

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×