<p>It's bad practice to use recursion when a while loop would suffice. Recursive code is harder to read, harder to debug, harder for the compiler to optimize and it may cause a stack overflow.</p>
<p>Besides, connections shouldn't be made from the main thread.</p>
<p>EDIT: Maybe it's because there's no limit of trials after which the code should assume that the user went offline. Otherwise the stack will grow and if the stack doesn't overflow the iterator may go out-of-bounds.</p>

CC BY-SA 4.0
2022-01-01T13:39:53.430Z
2022-01-01T14:52:53.083Z
2022-01-01T14:52:53.083Z
MCCCS
<blockquote>
<p>Is the limitation about the fact that &quot;except Exception&quot; excepts nearly everything and that is not a good practice?</p>
</blockquote>
<p>I don't think &quot;except Exception&quot; is bad practice necessarily. Maybe recursing immediately on it happening, like this snippet, is.</p>
<blockquote>
<p>Would putting a security (like don't call recursively indefinitely) be a solution?</p>
</blockquote>
<p>Yes, but such security would not be a &quot;don't call recursively indefinitely&quot; if Python <a href="https://en.wikipedia.org/wiki/Python_(programming_language)#Statements_and_control_flow" rel="nofollow noreferrer">supported</a> <a href="https://en.wikipedia.org/wiki/Tail_call" rel="nofollow noreferrer">tail call optimization</a>. You can mind that if you see a snippet like this in a different programming language.</p>

CC BY-SA 4.0
2022-01-20T06:38:38.700Z
2022-01-20T06:38:38.700Z
Mercedes
<p><a href="http://sebastianappelt.com/understanding-blockchain-peer-discovery-and-establishing-a-connection-with-python/" rel="nofollow noreferrer">This link</a> looks very practical and &quot;hands in the dirty&quot;. But in the example of Python code for node discovery and connections, it often gives warning about the fact it is of &quot;educational purposes&quot; and not real world.</p>
<p>Could you please explain what the caveats of the implementation showed in the link are, and how to correct them in the real world:</p>
<p>Example of such limitations highlighted:</p>
<p><a href="https://i.sstatic.net/Q32u7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Q32u7.png" alt="Example of code" /></a></p>
<p>Is the limitation about the fact that &quot;except Exception&quot; excepts nearly everything and that is not a good practice?
Would putting a security (like don't call recursively indefinitely) be a solution?</p>

<python><peer-discovery><source-code>
Why are there limitations between educational and real world usage?
totalMongot
# Why are there limitations between educational and real world usage?

(Asked by: username_0 on 2022-01-01)

[This link](http://sebastianappelt.com/understanding-blockchain-peer-discovery-and-establishing-a-connection-with-python/) looks very practical and "hands in the dirty". But in the example of Python code for node discovery and connections, it often gives warning about the fact it is of "educational purposes" and not real world.

Could you please explain what the caveats of the implementation showed in the link are, and how to correct them in the real world:

Example of such limitations highlighted:

[![Example of code](https://i.sstatic.net/Q32u7.png)](https://i.sstatic.net/Q32u7.png)

Is the limitation about the fact that "except Exception" excepts nearly everything and that is not a good practice?
Would putting a security (like don't call recursively indefinitely) be a solution?

**Tags:** python, peer-discovery, source-code


---

## 2 answers

### Answer by username_1 on 2022-01-01. Score: 2

It's bad practice to use recursion when a while loop would suffice. Recursive code is harder to read, harder to debug, harder for the compiler to optimize and it may cause a stack overflow.

Besides, connections shouldn't be made from the main thread.

EDIT: Maybe it's because there's no limit of trials after which the code should assume that the user went offline. Otherwise the stack will grow and if the stack doesn't overflow the iterator may go out-of-bounds.


---

### Answer by username_2 on 2022-01-20. Score: 0

> Is the limitation about the fact that "except Exception" excepts nearly everything and that is not a good practice?

I don't think "except Exception" is bad practice necessarily. Maybe recursing immediately on it happening, like this snippet, is.

> Would putting a security (like don't call recursively indefinitely) be a solution?

Yes, but such security would not be a "don't call recursively indefinitely" if Python [supported](https://en.wikipedia.org/wiki/Python_(programming_language)#Statements_and_control_flow) [tail call optimization](https://en.wikipedia.org/wiki/Tail_call). You can mind that if you see a snippet like this in a different programming language.


---

