should "self" be changed?

Chris Angelico rosuav at gmail.com
Wed May 27 02:32:48 EDT 2015


On Wed, May 27, 2015 at 3:23 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Wednesday 27 May 2015 06:45, Mark Lawrence wrote:
>
>> Apart from breaking all the tools that rely on "self" being spelt "self"
>> this looks like an excellent idea.
>
> Tools which rely on self being spelled "self" are already broken. It's a
> convention, nothing more, and there are various good reasons for breaking
> the convention:
>
> - metaclasses
> - classmethods
> - custom descriptors
> - nested classes

If it truly relies on it, yes. But if I see a function that has "self"
as its first parameter, I'm going to read it as a (probable) method
rather than a stand-alone function, and it wouldn't surprise me if
introspection and/or source code parsing made the same assumption.
It's as strong a convention as "don't touch the ones that begin with
an underscore", which is broken by the namedtuple due to namespacing
requirements, but otherwise is fairly dependable (eg if tab completion
skipped the underscore attributes, it'd be a bit less useful on
namedtuples, but probably more practical overall). Using some other
name in place of "self" should definitely remain *possible*, but not
commonly done.

ChrisA



More information about the Python-list mailing list