[Python-ideas] Mitigating 'self.' Method Pollution

Ron Adam ron3200 at gmail.com
Sat Jul 11 18:59:51 CEST 2015

On 07/11/2015 04:38 AM, Chris Angelico wrote:
> On Sat, Jul 11, 2015 at 6:33 PM, Ben Finney<ben+python at benfinney.id.au>  wrote:
>>> >>And, for folks who still prefer to prefix all their field references
>>> >>with 'self.', the proposal in no way prevents them from doing so. It
>>> >>merely allows the rest of us to be a bit less wordy and more pithy in
>>> >>our code.
>> >
>> >Python requires explicit declaration of the namespace for names. That
>> >protects me from ambiguities that would otherwise be very common in
>> >other people's code. I appreciate that and do not want it threatened.
> Not quite true; Python requires explicit declaration of names when
> they're bound to, but is quite happy to do a scope search (local,
> nonlocal, global, builtin) for references. But the rules are fairly
> simple. Aside from the possibility that someone imports your module
> and sets module.len to shadow a builtin, everything can be worked out
> lexically by looking for the assignments.

I think some don't realise the names in a class block are not part of the 
static scope of the methods defined in that same class block.  The methods 
get the static scope the class is defined in, but that excludes the names 
in the class block.  If a class inherits methods defined in another module, 
those methods get the static scope where they were defined, and the methods 
local to the child class get a completely different static scope.   But 
usually it's not a problem, thanks to "self".  ;-)


More information about the Python-ideas mailing list