Quirk difference between classes and functions

Gregory Ewing greg.ewing at canterbury.ac.nz
Tue Feb 26 01:00:27 EST 2019


Chris Angelico wrote:
> Classes and functions behave differently. Inside a function, a name is
> local if it's ever assigned to; but in a class, this is not the case.

Actually, it is. Assigning to a name in a class body makes it part
of the class namespace, which is the local namespace at the time
the class body is executed.

The unusual thing about a class namespace is that it doesn't
form part of the scope of closures created within the class.
So methods, for example, can't directy see attributes of the
class they're defined in.

-- 
Greg


More information about the Python-list mailing list