Python loop in Functions - python

This is my nested functions code
def get_id():
for i in range(1,100):
pass
return i
def get_id_mysql(x):
print(x)
variable = get_id()
get_id_mysql(variable)
this function get_id return ( output : 1 ) and loop stop.. how can I hand over full loop? I mean 1,2,3..99

I Found solutions
def get_id():
t = []
for i in range(1,100):
t.append(i)
return t
def get_id_mysql(x):
for i in x:
print(i)
variable = get_id()
get_id_mysql(variable)

Related

Making a generator out of an inner function

I would like to extract data from an inner function and build a generator to pass this data somewhere else.
I have the following code:
def process_func(x):
# how to construct a generator out of all arguments passed
# to this function in the consecutive calls?
print(f"processing {x}")
return True
def save_func(x):
print(f'saving {x}')
# don't edit
def read_data_and_apply_func(func):
for x in range(5):
func(x)
# don't edit
def save_data(generator, save_func):
for i in generator:
save_func(i)
def main():
generator = () # generator should contain all data passed in all read_data_and_apply_func
read_data_and_apply_func(process_func)
save_data(generator, print)
main()
how do I edit just the main function (and maybe process_func) so that I can pass the data from the inside of read_data_and_apply_func to save_data?
I tried writing code like this:
def create_generator(read_data_and_apply_func, process_func):
def generator():
for x in read_data_and_apply_func(generator.send):
yield x
return generator
def main():
generator = create_generator(read_data_and_apply_func, process_func)
generator_instance = generator()
save_data(generator_instance)
and a few similar snippets but neither of them worked.
I think the desired output should be:
processing 0
saving 0
processing 1
saving 1
processing 2
saving 2
processing 3
saving 3
processing 4
saving 4
Here's one way. This doesn't interleave the output lines.
class Holder:
def __init__(self,hdlr):
self.data = []
self.hdlr = hdlr
def add(self,i):
self.data.append(i)
self.hdlr(i)
def __iter__(self):
return iter(self.data)
def process_func(x):
print(f"processing {x}")
return True
def save_func(x):
print(f'saving {x}')
# don't edit
def read_data_and_apply_func(func):
for x in range(5):
func(x)
# don't edit
def save_data(generator, save_func):
for i in generator:
save_func(i)
def main():
generator = Holder(process_func)
read_data_and_apply_func(generator.add)
save_data(generator, save_func)
main()

Explain how probSecond.calls equal to zero

def MainCount(f):
def progFirst(*args,**kwargs):
progFirst.calls+=1
return f(*args,**kwargs)
progFirst.calls=0
return progFirst
#MainCount
def progSecond(i):
return i+1
#MainCount
def Count(i=0,j=1):
return i*j+1
print(progSecond.calls)
for n in range(5):
progSecond(n)
Count(j=0,i=1)
print(Count.calls)
Output :0
1
As per my understanding MainCount(probSecond) but I am not understant then how probSecond.calls equal to zero same in Count.calls also
As You Can See in MainCount function probFirst.Calls is attribute of function .When MainCount(probSecond) Now probSecond.calls is also attribute of MainCount function.
# A Python example to demonstrate that
# decorators can be useful attach data
# A decorator function to attach
# data to func
def attach_data(func):
func.data = 3
return func
#attach_data
def add (x, y):
return x + y
# Driver code
# This call is equivalent to attach_data()
# with add() as parameter
print(add(2, 3))
print(add.data)

Access variable of outer function from inner function

I want to use a construct like this, where a function is defined inside of another and can alter a value defined in the outer function:
def function1():
res = []
def function2():
global res
if (possibleToAnswer):
res.append(answer)
else:
function2()
return res
print (("%s") % function1(para))
It doesn't seem to work. I keep getting unbound bug. Any idea about how to get it to work?
Don't use global—it's not in the immediate scope of function2, but it's not global.
def function1():
res = []
def function2():
if (possibleToAnswer):
res.append(answer)
else:
function2()
return res
print (("%s") % function1(para))

Get inner function result without interaction of outer function in python

I want to get inner function result so i code it like
def main():
def sub():
a = 1
print a
exec main.__code__.co_consts[1]
using above code working successfully but i want to pass the argument to the sub function like...
def main():
def sub(x):
a = x + 1
return a
ans = exec main.__code__.co_consts[1]
print ans
in that problem is i don't know how to pass that x value.
that work must need to exec so that how to pass that x value with exec without interaction of main function
Maybe something like the code below, as suggested by this SO answer
def main():
def sub():
a = x + 1
print a
return a
exec(main.__code__.co_consts[1], {'x': 1} )

recursion within a class

I am trying to place a recursive formula inside a class statement
class SomeNode:
def __init__(self, a):
leng = len(a)
half= leng/2
self.firstnode=a[0][0]
self.child1=SomeNode([a[i]for k in range(leng)])
self.child2=SomeNode([a[j] for j in range(leng)])
def recursfunc(self):
print self.firstnode
recursfunc(self.child1)
recursfunc(self.child2)
However I keep getting the error message NameError: global name 'recursfunc' is not defined
You need to use self.recursfunc()
def tri_recursion(k):
if(k>0):
result = k+tri_recursion(k-1)
print(result)
else:
result = 0
return result
print("\n\nRecursion Example Results")
tri_recursion(7)

Categories