[Python-Dev] Tangent on class level scoping rules (was Re: PEP 463: Exception-catching expressions)
Eric Snow
ericsnowcurrently at gmail.com
Fri Feb 21 22:55:54 CET 2014
On Fri, Feb 21, 2014 at 7:42 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> It's a relatively arcane scoping rule that only matters if you have
> non-trivial logic at class scope. The vast majority of Python
> programmers will never have to care, because they do the typical thing
> and their class bodies consist almost entirely of function definitions
> and relatively simple assignment statements.
That is definitely an esoteric corner. It's only really bitten me
when I was doing stuff with nested classes [1] and mined too deeply.
Here's a simple example:
class Spam:
class Ham:
A = None
B = None
class Eggs:
class Bacon(Ham):
A = 3
If I recall correctly, Larry Hastings ran into something similar a while back.
-eric
[1] The nested classes were used just for easy-to-read namespaces,
effectively hijacking the class definition syntax with no intention of
actually using the class as a type.
More information about the Python-Dev
mailing list