More pythonic way to nest functions
<p>Here is the code that I have:</p>

<pre><code>def reallyquit():
        from easygui import boolbox
        if __debug__: print ("realy quit?")
        answer = boolbox("Are you sure you want to Quit?")
        if answer == 1:
            sys.exit()
        return

columns = ['adr1','adr2','spam','spam', 'spam']

Street = choicebox("Street Address?", title, columns)
    while Street == None:
        reallyquit()
        Street = choicebox("Street Address?", title, columns)
</code></pre>

<p>Is there a more pythonic way to recursively ask a question if a user closes a box inadvertently? Mainly looking for tips on the last 4 lines, but feel free to critique the rest.</p>

<pre class="lang-py prettyprint-override"><code>import sys
from easygui import boolbox, choicebox

def reallyquit():
    "Ask if the user wants to quit. If so, exit."
    if __debug__: 
        print("really quit?")
    answer = boolbox("Are you sure you want to Quit?")
    if answer == 1:
        sys.exit()

columns = ['adr1', 'adr2', 'spam', 'spam']
street = None

# ask for address until given or user decides to quit
while street is None:
    street = choicebox("Street Address?", title, columns)
    if street is None:
        reallyquit()
</code></pre>

<p>In the main logic: Usually you would not test if a value is equal to <code>None</code>, since it is a singleton (only one in the system, and all occurrences of <code>None</code> are to the same logical <code>None</code>). Thus <code>is None</code> is a better test. I've also used a little more Pythonic code formatting (spaces after commas in a list, lowercase variable names), and a test order that doesn't duplicate the user interaction call.</p>

<p>An alternative form suggested by <a href="http://stackoverflow.com/users/102441/eric">Eric</a> would be something like:</p>

<pre><code>while True:
    street = choicebox("Street Address?", title, columns)
    if street is not None:
        break
    reallyquit()
</code></pre>

<p>This has the virtue of not repeating the <code>street is None</code> test, but is a bit longer. Your preference for which option seems clearer and more logical may vary. </p>

<p>For the preparations: Imports are taken out of functions, and <code>choicebox</code> is imported where it was previously missing. The function is given
a defining comment string. The one statement per line standard is enforced. And the unnecessary <code>return</code> removed.</p>

<p>This is still a fairly coarse program, but it's now "more Pythonic."</p>

<p>Not to be overwhelming, but the standard for Pythonic code appearance is <a href="https://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP 8</a>. There are tools such as <a href="https://pypi.python.org/pypi/pep8" rel="nofollow">pep8</a> you can install to check compliance. The deeper or "semantic" part of Pythonic code is sketched out in <a href="https://www.python.org/dev/peps/pep-0020/" rel="nofollow">PEP 20</a>, also known as "The Zen of Python." There is, sadly, no tool to judge compliance with those deeper principles. Experience is required.</p>

python python-2.7 easygui