Note that there are some explanatory texts on larger screens.

plurals
  1. POPython module order of execution
    text
    copied!<p>I have a normally structured Python program with import statements, class definitions, other routines, and some "main" statements that invoke methods in the classes (in that order). a print statement after the imports prints "ok"</p> <p>Python 2.7.2</p> <p>I am getting a nameError in a class method.</p> <pre><code>print &gt;&gt; common, ... NameError: 'common' is not defined </code></pre> <p><code>common</code> is used earlier in the same method, but the earlier references occasioned no errors.</p> <p><code>common</code> is used in many methods - moving this method physically has no effect: the error is still on the same line in this method.</p> <p>The error occurs <em>before</em> the method is called, and before any of the "main" statements are executed. Putting return as the first executable statement in the method has no effect. This is all apparently happening at class definition time. </p> <p>If I comment out the <code>print &gt;&gt; common</code> statements, I get a different NameError in the same method. </p> <p>I have no idea how I can get a NameError inside a method at 'definition time'.</p> <p>Any ideas? The method is below:</p> <p>x = z ** 2 below is <em>supposed</em> to generate a NameError: there is no z. A print statement just after all the function definitions is not executed.</p> <pre><code>"""code below""" @classmethod def show_role_map(cls): """show jobs within roles, with total days, with percents of totals""" return raise ZeroDivisionError return print &gt;&gt; common, "xyzzy" x = z ** 2 p = Performance("Traveler: show_role_map") print "\tshow_role_map" roles = cls.role_map.keys() roles.sort() header ("Qualitative Role Map") role_totals = collections.defaultdict(float) job_totals = collections.defaultdict(float) for name in Traveler.roster: trav = Traveler.roster[name] for day in trav.roles: frac = 1.0 / len(trav.roles[day]) for role in trav.roles[day]: role_totals[role] += frac for day in trav.jobs: frac = 1.0 / len(trav.jobs[day]) for job in trav.jobs[day]: job_totals[job] += frac role_total = sum(role_totals.values()) job_total = sum(job_totals.values()) assert abs(role_total - job_total) &lt;= 1e-6 print &gt;&gt; common, "Total Role days =", role_total print &gt;&gt; common, "Total Job days =", job_total print &gt;&gt; common for role in roles: if role_totals[role] == 0: continue print &gt;&gt; common, "\t%12s %51.1f %12s %12.3f" %\ (role, role_totals[role], \ "", 100.0 * role_totals[role] / role_total) jobs = list(cls.role_map[role]) jobs.sort (key = lambda x: (job_totals[x], x), reverse = True) for index, job in enumerate(jobs, 1): if job_totals[job] == 0: continue print role, job, role_totals[role], job_totals[job] print &gt;&gt; common, "\t\t%6d. %35s %12.1f % 12.3f %12.3f" % \ (index, job, job_totals[job], \ 100.0 * job_totals[job] / role_totals[role], 100.0 * job_totals[job] / role_total) print &gt;&gt; common print &gt;&gt; common, "\n", "_" * 60, "\n" #--ERROR OCCURS FOR THIS LINE **************** print &gt;&gt; common, "\nRoles in Total Tripday order\n" roles = role_totals.keys() roles.sort (order = lambda x: (role_totals[x], x), reverse = True) for index, role, in enumerate(roles,1): print &gt;&gt; common, "%6d. %15s %12.1f %12.3f" % \ (index, role, role_totals[role], \ 100.0 * role_totals[role]/role_total) print &gt;&gt; common, "\n", "_" * 60, "\n" print &gt;&gt; common, "\nDetailed Trip Roles in Total Tripday order" jobs = job_totals.keys() jobs.sort (key = lambda x: (job_totals[x], x), reverse = True) for index, job in jobs: print &gt;&gt; common, "%6d. %35s %12.1f %12.3f" % \ (index, job, job_totals[job], 100.0 * job_totals[job] / job_total) p.close() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload