本文介绍了递归调用和堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个程序可以轻易找到与

点重叠的对象。水平和垂直搜索从

内部递归调用。

这种实现方式是用本地

变量填充堆栈空间在每次通话中。如果这不好,有没有更好的方法实现?b $ b?或者python本身会理解调用在最后一行发生了

,所以局部变量不需要被推入

堆栈?


def find_point(pt):

返回_hor_search(pt,random_obj)

def _hor_search(pt,obj):

...

object = ...

...

如果物品满足某些条件:

返回对象

else:

返回_ver_search(pt,object)

def _ver_search(pt,obj):

...

object = ...

...

如果对象满足某些条件:

返回对象

否则:

返回_hor_search(pt,object)

-

Suresh

解决方案



我恐怕没有,调用将被堆叠,直到找到一些对象。

Python不做尾递归优化" (至少,我不知道

)。但即使它可以做到这一点,在这种情况下你会在两个函数之间进行递归

调用,而且这有点难度。


回到你原来的问题,也许你可以从计算几何中使用一些已知的

算法;从


-

Gabriel Genellina




我不敢,调用将被堆叠,直到找到一些对象。

Python不做尾递归优化" (至少,我不知道

)。但即使它可以做到这一点,在这种情况下你会在两个函数之间进行递归

调用,而且这有点难度。


回到你原来的问题,也许你可以从计算几何中使用一些已知的

算法;从

-

Gabriel Genellina



谢谢Gabriel的回复。


我可以将电话堆叠起来,但我想知道当前的

变量是否堆叠,因为返回语句后跟着

函数调用?


def test():

x = 22

y = 33

z = x + y

返回anotherFunction(z)


在此函数中将所有局部变量(x,y,z)在调用anotherFunction(z)之前将其推入堆栈中,或者Python会发现

不再需要本地人,因为anotherFunction(z)只是

退回?


-

Suresh



Hi,
I have a program which literately finds the object that overlapping a
point. The horizontal and vertical search are called recursively from
inside each other.
Is this way of implementation fill the stack space with the local
variables inside each call. If this is not good, is there a better way
to implement? Or python itself will understand that the calls happen
in the last line, so local variables need not be pushed into the
stack?

def find_point(pt):
return _hor_search(pt, random_obj)

def _hor_search(pt, obj):
...
object = ...
...
if object meets some condition:
return object
else:
return _ver_search(pt, object)

def _ver_search(pt, obj):
...
object = ...
...
if object meets some condition:
return object
else:
return _hor_search(pt, object)
-
Suresh

解决方案

I''m afraid not, the calls will be stacked until some object is found.
Python does not do "tail recursion optimization" (at least, I''m not aware
of that). But even if it could do that, in this case you have recursive
calls between two functions, and that''s a bit harder.

Going back to your original problem, maybe you can use some known
algorithms from computational geometry; start with
http://www.faqs.org/faqs/graphics/algorithms-faq/

--
Gabriel Genellina



I''m afraid not, the calls will be stacked until some object is found.
Python does not do "tail recursion optimization" (at least, I''m not aware
of that). But even if it could do that, in this case you have recursive
calls between two functions, and that''s a bit harder.

Going back to your original problem, maybe you can use some known
algorithms from computational geometry; start with http://www.faqs.org/faqs/graphics/algorithms-faq/

--
Gabriel Genellina

Thanks Gabriel for the response.

I am OK with calls being stacked, but I wondering will the local
variables be stacked given that return statement is followed by the
function call?

def test():
x = 22
y = 33
z = x+y
return anotherFunction(z)

In this function will all the local variables (x,y,z) be pushed into
the stack before calling anotherFunction(z) or Python will find out
that the locals are no longer needed as anotherFunction(z) is just
returned?

-
Suresh


I found a simpler solution: get rid of recursive calls; using while
loops.

-
Suresh


这篇关于递归调用和堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:13