2007-05-01 15:35:45 -04:00
|
|
|
PEP: 3130
|
|
|
|
Title: Access to Current Module/Class/Function
|
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
|
|
|
Author: Jim J. Jewett <jimjjewett@gmail.com>
|
2007-05-10 18:56:48 -04:00
|
|
|
Status: Rejected
|
2007-05-01 15:35:45 -04:00
|
|
|
Type: Standards Track
|
2017-02-02 12:58:49 -05:00
|
|
|
Content-Type: text/x-rst
|
2007-05-01 15:35:45 -04:00
|
|
|
Created: 22-Apr-2007
|
|
|
|
Python-Version: 3.0
|
|
|
|
Post-History: 22-Apr-2007
|
|
|
|
|
|
|
|
|
2007-05-10 18:56:48 -04:00
|
|
|
Rejection Notice
|
2017-02-02 12:58:49 -05:00
|
|
|
================
|
2007-05-10 18:56:48 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
This PEP is rejected. It is not clear how it should be
|
|
|
|
implemented or what the precise semantics should be in edge cases,
|
|
|
|
and there aren't enough important use cases given. response has
|
|
|
|
been lukewarm at best.
|
2007-05-10 18:56:48 -04:00
|
|
|
|
|
|
|
|
2007-05-01 15:35:45 -04:00
|
|
|
Abstract
|
2017-02-02 12:58:49 -05:00
|
|
|
========
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
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__``.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
Rationale for ``__module__``
|
|
|
|
============================
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
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.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
::
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
if __name__ == "__main__": ...
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
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.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
::
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
# __import__ lets you use a variable, but... it gets more
|
|
|
|
# complicated if the module is in a package.
|
|
|
|
__import__(__name__)
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
# So just go to sys modules... and hope that the module wasn't
|
|
|
|
# hidden/removed (perhaps for security), that __name__ wasn't
|
|
|
|
# changed, and definitely hope that no other module with the
|
|
|
|
# same name is now available.
|
|
|
|
class X(object):
|
|
|
|
pass
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
import sys
|
|
|
|
mod = sys.modules[__name__]
|
|
|
|
mod = sys.modules[X.__class__.__module__]
|
|
|
|
|
|
|
|
Proposal: Add a ``__module__`` keyword which refers to the module
|
|
|
|
currently being defined (executed). (But see open issues.)
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
# XXX sys.main is still changing as draft progresses. May
|
|
|
|
# really need sys.modules[sys.main]
|
|
|
|
if __module__ is sys.main: # assumes PEP (3122), Cannon
|
|
|
|
...
|
2007-05-01 15:35:45 -04:00
|
|
|
|
|
|
|
|
2017-04-05 12:14:26 -04:00
|
|
|
Rationale for ``__class__``
|
|
|
|
===========================
|
2017-02-02 12:58:49 -05:00
|
|
|
|
|
|
|
Class methods are passed the current instance; from this they can
|
|
|
|
determine ``self.__class__`` (or cls, for class methods).
|
|
|
|
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.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
::
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
class C(B):
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
def meth(self):
|
|
|
|
super(C, self).meth() # Hope C is never rebound.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
class D(C):
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
def meth(self):
|
|
|
|
# ?!? issubclass(D,C), so it "works":
|
|
|
|
super(C, self).meth()
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
Proposal: Add a ``__class__`` keyword which refers to the class
|
|
|
|
currently being defined (executed). (But see open issues.)
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
::
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
class C(B):
|
|
|
|
def meth(self):
|
|
|
|
super(__class__, self).meth()
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
Note that super calls may be further simplified by the "New Super"
|
|
|
|
PEP (Spealman). 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.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
Note that ``__class__`` (or ``__this_class__``) is not quite the same as
|
|
|
|
the ``__thisclass__`` property on bound super objects. The existing
|
2017-04-05 12:14:26 -04:00
|
|
|
``super.__thisclass__`` property refers to the class from which the
|
2017-02-02 12:58:49 -05:00
|
|
|
Method Resolution Order search begins. In the above class D, it
|
|
|
|
would refer to (the current reference of name) C.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
Rationale for ``__function__``
|
|
|
|
==============================
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
Functions (including methods) often want access to themselves,
|
|
|
|
usually for a private storage location or true recursion. While
|
|
|
|
there are several workarounds, all have their drawbacks.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
::
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
def counter(_total=[0]):
|
|
|
|
# _total shouldn't really appear in the
|
|
|
|
# signature at all; the list wrapping and
|
|
|
|
# [0] unwrapping obscure the code
|
|
|
|
_total[0] += 1
|
|
|
|
return _total[0]
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
@annotate(total=0)
|
|
|
|
def counter():
|
|
|
|
# Assume name counter is never rebound:
|
|
|
|
counter.total += 1
|
|
|
|
return counter.total
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
# class exists only to provide storage:
|
|
|
|
class _wrap(object):
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
__total = 0
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
def f(self):
|
|
|
|
self.__total += 1
|
|
|
|
return self.__total
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
# set module attribute to a bound method:
|
|
|
|
accum = _wrap().f
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
# This function calls "factorial", which should be itself --
|
|
|
|
# but the same programming styles that use heavy recursion
|
|
|
|
# often have a greater willingness to rebind function names.
|
|
|
|
def factorial(n):
|
|
|
|
return (n * factorial(n-1) if n else 1)
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
Proposal: Add a ``__function__`` keyword which refers to the function
|
|
|
|
(or method) currently being defined (executed). (But see open
|
|
|
|
issues.)
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
@annotate(total=0)
|
|
|
|
def counter():
|
|
|
|
# Always refers to this function obj:
|
|
|
|
__function__.total += 1
|
|
|
|
return __function__.total
|
|
|
|
|
|
|
|
def factorial(n):
|
|
|
|
return (n * __function__(n-1) if n else 1)
|
2007-05-01 15:35:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
Backwards Compatibility
|
2017-02-02 12:58:49 -05:00
|
|
|
=======================
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
While a user could be using these names already, double-underscore
|
|
|
|
names ( ``__anything__`` ) are explicitly reserved to the interpreter.
|
|
|
|
It is therefore acceptable to introduce special meaning to these
|
|
|
|
names within a single feature release.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
Implementation
|
2017-02-02 12:58:49 -05:00
|
|
|
==============
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
Ideally, these names would be keywords treated specially by the
|
|
|
|
bytecode compiler.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
Guido has suggested [1]_ using a cell variable filled in by the
|
|
|
|
metaclass.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
Michele Simionato has provided a prototype using bytecode hacks [2]_.
|
|
|
|
This does not require any new bytecode operators; it just
|
|
|
|
modifies the which specific sequence of existing operators gets
|
|
|
|
run.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
Open Issues
|
2017-02-02 12:58:49 -05:00
|
|
|
===========
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
- 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__``).
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
- Are all three keywords needed, or should this enhancement be
|
|
|
|
limited to a subset of the objects? Should methods be treated
|
|
|
|
separately from other functions?
|
2007-05-01 15:35:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
References
|
2017-02-02 12:58:49 -05:00
|
|
|
==========
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
.. [1] Fixing super anyone? Guido van Rossum
|
|
|
|
http://mail.python.org/pipermail/python-3000/2007-April/006671.html
|
2007-05-01 15:35:45 -04:00
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
.. [2] Descriptor/Decorator challenge, Michele Simionato
|
|
|
|
http://groups.google.com/group/comp.lang.python/browse_frm/thread/a6010c7494871bb1/62a2da68961caeb6?lnk=gst&q=simionato+challenge&rnum=1&hl=en#62a2da68961caeb6
|
2007-05-01 15:35:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
2017-02-02 12:58:49 -05:00
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
2007-05-01 15:35:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-02 12:58:49 -05:00
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
sentence-end-double-space: t
|
|
|
|
fill-column: 70
|
|
|
|
coding: utf-8
|
|
|
|
End:
|