# HG changeset patch
# User Florent Hivert <Florent.Hivert@univ-rouen.fr>
# Date 1266075432 -3600
# Node ID 7fc2c455fdc1b230f6ed52c26f293485dab73b71
# Parent f5eba38970c2e43a49a713168fc5615ed8882177
#8250: Improved Doc + Added (ClassCall|Nested)Metaclass to the reference manual.
diff --git a/doc/en/reference/misc.rst b/doc/en/reference/misc.rst
a
|
b
|
Miscellaneous |
30 | 30 | sage/misc/random_testing |
31 | 31 | sage/misc/sagedoc |
32 | 32 | sage/rings/arith |
| 33 | sage/misc/nested_class |
| 34 | sage/misc/classcall_metaclass |
diff --git a/sage/misc/classcall_metaclass.py b/sage/misc/classcall_metaclass.py
a
|
b
|
|
1 | 1 | r""" |
2 | | ClasscallMetaclass |
| 2 | Special Methods for Classes. |
3 | 3 | """ |
4 | 4 | #***************************************************************************** |
5 | 5 | # Copyright (C) 2009 Nicolas M. Thiery <nthiery at users.sf.net> |
… |
… |
class ClasscallMetaclass(NestedClassMeta |
31 | 31 | - ``.__classget__`` for customizing the binding behavior in |
32 | 32 | ``foo.cls`` (analogue of ``.__get__``). |
33 | 33 | |
34 | | See the documentation of :meth:`.__classcall__`` and of |
35 | | :meth:`.__classget`` for the description of the respective protocol. |
| 34 | See the documentation of :meth:`.__call__`` and of |
| 35 | :meth:`.__get__`` for the description of the respective protocols. |
36 | 36 | |
37 | 37 | TODO: find a good name for this metaclass. |
38 | 38 | |
… |
… |
class ClasscallMetaclass(NestedClassMeta |
68 | 68 | ``obj.Inner(...)`` is equivalent to ``Outer.Inner(obj, ...)``:: |
69 | 69 | |
70 | 70 | sage: import functools |
| 71 | sage: from sage.misc.nested_class import NestedClassMetaclass |
71 | 72 | sage: from sage.misc.classcall_metaclass import ClasscallMetaclass |
72 | 73 | sage: class Outer: |
| 74 | ... __metaclass__ = NestedClassMetaclass # workaround for python pickling bug |
| 75 | ... |
73 | 76 | ... class Inner(object): |
74 | 77 | ... __metaclass__ = ClasscallMetaclass |
75 | 78 | ... @staticmethod |
… |
… |
class ClasscallMetaclass(NestedClassMeta |
83 | 86 | ... self.instance = instance |
84 | 87 | sage: obj = Outer() |
85 | 88 | sage: bar = obj.Inner() |
86 | | calling __classget__(<class '__main__.Inner'>, <__main__.Outer instance at 0x...>, __main__.Outer) |
| 89 | calling __classget__(<class '__main__.Outer.Inner'>, <__main__.Outer object at 0x...>, <class '__main__.Outer'>) |
87 | 90 | sage: bar.instance == obj |
88 | 91 | True |
89 | 92 | |
90 | 93 | Calling ``Outer.Inner`` returns the (unbinded) class as usual:: |
91 | 94 | |
92 | 95 | sage: Inner = Outer.Inner |
93 | | calling __classget__(<class '__main__.Inner'>, None, __main__.Outer) |
| 96 | calling __classget__(<class '__main__.Outer.Inner'>, None, <class '__main__.Outer'>) |
94 | 97 | sage: Inner |
95 | | <class '__main__.Inner'> |
| 98 | <class '__main__.Outer.Inner'> |
96 | 99 | sage: type(bar) is Inner |
97 | 100 | True |
98 | 101 | |
… |
… |
class ClasscallMetaclass(NestedClassMeta |
101 | 104 | ..warning:: calling ``obj.Inner`` does no longer return a class:: |
102 | 105 | |
103 | 106 | sage: bind = obj.Inner |
104 | | calling __classget__(<class '__main__.Inner'>, <__main__.Outer instance at ...>, __main__.Outer) |
| 107 | calling __classget__(<class '__main__.Outer.Inner'>, <__main__.Outer object at 0x...>, <class '__main__.Outer'>) |
105 | 108 | sage: bind |
106 | 109 | <functools.partial object at 0x...> |
107 | 110 | """ |
diff --git a/sage/misc/nested_class.py b/sage/misc/nested_class.py
a
|
b
|
|
1 | 1 | """ |
| 2 | Fixing Pickle for Nested Classes. |
| 3 | |
2 | 4 | As of Python 2.6, names for nested classes are set by Python in a |
3 | 5 | way which is incompatible with the pickling of such classes (pickling by name):: |
4 | 6 | |