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

Chris Angelico rosuav at gmail.com
Sat Jul 11 17:18:08 CEST 2015

On Sun, Jul 12, 2015 at 1:07 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, Jul 11, 2015 at 06:38:34PM +1000, 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,
> No it doesn't. It only requires an explicit declaration of names when
> you don't wish to use the default *implicit* rule that any name you bind
> to is a local.

Sorry, that was sloppily worded. I was making the point that Python
does _not_ require declarations when names are referenced, but in the
process implied that Python _does_ require them when they're bound to,
which as you say is not always the case. But it is still true that
Python requires explicit declarations _only_ when names are bound to.
In any case, I was arguing against a position which nobody actually
held, due to a misunderstanding of a previous post. We're all in
agreement that the search path is a Good Thing when referencing names.

(Side point: This is true of a lot of other search paths, too. Create
explicitly, reference implicitly. The command path on Unix or Windows,
the PostgreSQL schema search path, the Python module search
directories (sys.path), they're all looked up by unqualified name; but
when you create something, you usually have to say exactly where it
gets put. Sometimes there's a default (Postgres lets you implicitly
put things into the first location on the search path, which is what
you most commonly want anyway), sometimes not even that (creating a
file without a path name will put it in the current directory, which
on Unix is not in $PATH).)


More information about the Python-ideas mailing list