[Tutor] Regular User defined functions Vs Generator functions
Manprit Singh
manpritsinghece at gmail.com
Fri Oct 30 21:38:04 EDT 2020
Dear sir ,
I have a question , why not to prefer generator functions instead of
regular user defined functions, where i have problem like as that given
below :
I have to write a function that can return index of each and every
occurrence of substring w in string st1, st1 & w given below:
st1 = "I am a boy, i am an engineer, i am a genius"
w = "am"
If i have to write a function to solve this problem, i feel generator
function is more good( as compared to regular user defined function), that
i have written below :
def ind_word(st, w):
n = 0
while True:
n = st.find(w, n)
if n != -1:
yield n
else:
break
n += 1
st1 = "I am a boy, i am an engineer, i am a genius"
w = "am"
print(*ind_word(st1, w))
will give result
2 14 32
Need your suggestions.
Regards
Manprit Singh
More information about the Tutor
mailing list