Tuesday, 8 March 2022

Python#06

 Python Yield

yield is not like return. When return is used it is given to caller. When yield is used you can iterate, pass or suspend. Le us see with example. Ready guys! Start.. Here you go..

Here is the situation when you should use Yield instead of Return

Use yield instead of return when the data size is large

Yield is the best choice when you need your execution to be faster on large data sets.

Use yield when you want to return a big set of values to the calling function. 

Yield is an efficient way of producing data that is big or infinite.
yield wont take memory. 

Execution is faster The yield keyword in python works like a return with the only difference is that instead of returning a value, it gives back a generator function to the caller. 

A generator is a special type of iterator that, once used, will not be available again. 

The values are not stored in memory and are only available when called.

The values from the generator can be read using for-in, list() and next() method.

The main difference between yield and return is that yield returns back a generator function to the caller and return gives a single value to the caller.

Yield does not store any of the values in memory, and the advantage is that it is helpful when the data size is big, as none of the values are stored in memory.

The performance is better if the yield keyword is used in comparison to return for large data size.

# Yield is just like return but with generator(?) objects instead of values
Generators are special functions that have to be iterated to get the values
# To get the values the
# To get the values the objects has to be iterated
#Syntax yield expression
# #Sample yield function
# def demoyield():
# yield "Welcom to AMET - Pioneer in Maritime Education"
#
# out = demoyield()
# print(out)
# #<generator object demoyield at 0x000002802AC5DA50>
# # iterate to get the value in the above object
# #Generators are functions that return an iterable generator object. The values from the generator object are fetched one at a time instead of the full list together and hence to get the actual values you can use a for-loop, using next() or list() method
# for i in out:
#.      print(i)
# Example 2
# def gen():
#       yield 'G'
#.      yield 'A'
#       yield 'N'
#       yield 'D'
#       yield 'H'
#       yield 'I'
#       yield ' '
#       yield 'M'
#       yield 'A'
#       yield 'H'
#       yield 'A'
#       yield 'N'
#
# demo = gen()
#
# for i in demo:
#.      print(i)
#
#example 3 difference between normal function and generator function
#Normal
def normalfun():
     return 'Hello Zoox Robo Taxi to Universe - normal function'
def genfun():
       yield "'Hello Zoox Robo Taxi to Universe - generator function"

print(normalfun())
print(genfun())
# Hello Zoox Robo Taxi to Universe - normal function
#until it prints the whole string normalfun()wont stop
# <generator object genfun at 0x00000298E53AD900>
#but gen iterates so we can use the generator obhject to get the values and also , 'pause' and 'resume'
print(next(genfun()))
#Note next function
print(list(genfun()))

No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...