[Python-ideas] PEP 30xx: Access to Module/Class/Function Currently Being Defined (this)
jimjjewett at gmail.com
Mon Apr 23 05:05:14 CEST 2007
(Please note that several groups were Cc'd. For now, please limit
followups to python-3000. This would *probably* be backported to 2.6,
but that wouldn't be decided until the implementation strategy was
Title: Access to Module/Class/Function Currently Being Defined (this)
Author: Jim J. Jewett <jimjjewett at gmail.com>
Type: Standards Track
It is common to need a reference to the current module, class,
or function, but there is currently no entirely correct way to
do this. This PEP proposes adding the keywords __module__,
__class__, and __function__.
Many modules export various functions, classes, and other objects,
but will perform additional activities (such as running unit tests)
when run as a script. The current idiom is to test whether the
module's name has been set to magic value.
if __name__ == "__main__": ...
More complicated introspection requires a module to (attempt to)
import itself. If importing the expected name actually produces
a different module, there is no good workaround.
Proposal: Add a __module__ keyword which refers to the module
currently being defined (executed). (But see open issues.)
if __module__ is sys.main: ... # assuming PEP 3020, Cannon
Class methods are passed the current instance; from this they can
determine self.__class__ (or cls, for classmethods). Unfortunately,
this reference is to the object's actual class, which may be a
subclass of the defining class. The current workaround is to repeat
the name of the class, and assume that the name will not be rebound.
super(C, self).meth() # Hope C is never rebound.
super(C, self).meth() # ?!? issubclass(D,C), so it "works"
Proposal: Add a __class__ keyword which refers to the class currently
being defined (executed). (But see open issues.)
Note that super calls may be further simplified by PEP 30XX, Jewett.
The __class__ (or __this_class__) attribute came up in attempts to
simplify the explanation and/or implementation of that PEP, but was
separated out as an independent decision.
Note that __class__ (or __this_class__) is not quite the same as the
__thisclass__ property on bound super objects. The existing
super.__thisclass__ property refers to the class from which the Method
Resolution Order search begins. In the above class D, it would refer to
(the current reference of name) C.
Functions (including methods) often want access to themselves,
usually for a private storage location. While there are several
workarounds, all have their drawbacks.
def counter(_total=): # _total shouldn't really appear in the
_total += 1 # signature at all; the list wrapping and
return _total #  unwrapping obscure the code
counter.total += 1 # Assume name counter is never rebound
class _wrap(object): # class exists only to provide storage
self.__total += 1
accum=_wrap().f # set module attribute to a bound method
Proposal: Add a __function__ keyword which refers to the function
(or method) currently being defined (executed). (But see open issues.)
__function__.total += 1 # Always refers to this function obj
While a user could be using these names already, __anything__ names
are explicitly reserved to the interpreter. It is therefore acceptable
to introduce special meaning to these names within a single feature
Ideally, these names would be keywords treated specially by the bytecode
Guido has suggested  using a cell variable filled in by the metaclass.
Michele Simionato has provided a prototype using bytecode hacks .
- Are __module__, __class__, and __function__ the right names?
In particular, should the names include the word "this", either as
__this_module__, __this_class__, and __this_function__, (format
discussed on the python-3000 and python-ideas lists) or as
__thismodule__, __thisclass__, and __thisfunction__ (inspired by,
but conflicting with, current usage of super.__thisclass__).
- Are all three keywords needed, or should this enhancement be limited
to a subset of the objects? Should methods be treated separately from
 Fixing super anyone? Guido van Rossum
 Descriptor/Decorator challenge, Michele Simionato
This document has been placed in the public domain.
More information about the Python-ideas