[Tutor] Why is the name "self" optional instead of mandatory?

Danny Yoo dyoo at hashcollision.org
Thu Jan 21 01:15:52 EST 2016


> So I really only have one question:  Why not make Python's
> *traditional* name, "self", mandatory?  Why give the programmer this
> kind of choice?  [OK, that was two questions.]


There are situations where it might not be mandatory.


This is definitely not beginner Python material, so I don't want to
delve into it too deeply.  Roughly: we can do things like "metaclass"
programming where your programs modify the rules that construct
classes.  In that way, it's possible to do fairly crazy things.  We
might even be able to make it optional to say "self" all the time, if
we modify the rules radically enough.

If you are really interested in this, see:
https://jakevdp.github.io/blog/2012/12/01/a-primer-on-python-metaclasses/
for an primer.

Basically, Python does almost everything at run-time.  The sorts of
things we'd expect to be static properties aren't static in Python.
Other languages tend to lock things down a bit more firmly, or at the
very least, do more things at compile-time.

Python programmers have enormous flexibility at run-time, at the cost
of making it really easy to make mistakes that aren't caught until
run-time.


More information about the Tutor mailing list