1.1 --- a/docs/concepts.txt Sat Feb 26 01:12:25 2011 +0100
1.2 +++ b/docs/concepts.txt Sun Feb 27 02:45:38 2011 +0100
1.3 @@ -192,8 +192,8 @@
1.4 A limitation of this representation is that instance attributes may not shadow
1.5 class attributes: if an attribute with a given name is not defined on an
1.6 instance, an attribute with the same name cannot be provided by the class of
1.7 -the instance or any superclass of the instance's class. The exception to this
1.8 -restriction is the __class__ attribute, as described below.
1.9 +the instance or any superclass of the instance's class. This impacts the
1.10 +provision of the __class__ attribute, as described below.
1.11
1.12 The table can be compacted using a representation known as a displacement
1.13 list (referred to as an object list in this context):
1.14 @@ -236,20 +236,30 @@
1.15 The __class__ Attribute
1.16 -----------------------
1.17
1.18 -The exception to the above general rules about relative locations and absolute
1.19 -addresses involves the __class__ attribute which is defined differently for
1.20 -each class and its instances. Since the table elements can only refer to a
1.21 -single absolute address, thus providing only a single value, such absolute
1.22 -references which are sufficient for most class attributes would not be
1.23 -appropriate for the __class__ attribute. Using a common object-relative
1.24 -location of 0 permits the first attribute to be accessed via an object address
1.25 -regardless of whether a class or instance is involved.
1.26 +In Python 2.x, at least with new-style classes, instances have __class__
1.27 +attributes which indicate the class from which they have been instantiated,
1.28 +whereas classes have __class__ attributes which reference the type class.
1.29 +With the object table, it is not possible to provide absolute addresses which
1.30 +can be used for both classes and instances, since this would result in classes
1.31 +and instances having the same class, and thus the class of a class would be
1.32 +the class itself.
1.33
1.34 -Obviously, this requires both classes and instances to retain an attribute
1.35 -location specifically to hold the value appropriate for each object type,
1.36 -whereas a scheme which omits the __class__ attribute on classes would be able
1.37 -to employ an absolute address in the table and maintain only a single address
1.38 -to refer to the class for all instances.
1.39 +One solution is to use object-relative values in the table so that referencing
1.40 +the __class__ attribute of an instance produces a value which can be combined
1.41 +with an instance's address to yield the address of the attribute, which itself
1.42 +refers to the instance's class, whereas referencing the __class__ attribute of
1.43 +a class produces a similar object-relative value that is combined with the
1.44 +class's address to yield the address of the attribute, which itself refers to
1.45 +the special type class.
1.46 +
1.47 +Obviously, the above solution requires both classes and instances to retain an
1.48 +attribute location specifically to hold the value appropriate for each object
1.49 +type, whereas a scheme which omits the __class__ attribute on classes would be
1.50 +able to employ an absolute address in the table and maintain only a single
1.51 +address to refer to the class for all instances. The only problem with not
1.52 +providing a sensible __class__ attribute entry for classes would be the need
1.53 +for special treatment of __class__ to prevent inappropriate consultation of
1.54 +the table for classes.
1.55
1.56 Comparing Tables as Matrices with Displacement Lists
1.57 ----------------------------------------------------
1.58 @@ -289,11 +299,11 @@
1.59
1.60 Header.................................................... Attributes.................
1.61
1.62 - Identifier Identifier Address Identifier Size Object Object ...
1.63 + Identifier Identifier Address Identifier Size Object ...
1.64
1.65 - 0 1 2 3 4 5 6 7
1.66 - classcode attrcode/ invocation funccode size __class__ attribute ...
1.67 - instance reference reference reference
1.68 + 0 1 2 3 4 5 6
1.69 + classcode attrcode/ invocation funccode size attribute ...
1.70 + instance reference reference
1.71 status
1.72
1.73 Classcode
1.74 @@ -358,41 +368,52 @@
1.75
1.76 Class C:
1.77
1.78 - 0 1 2 3 4 5 6 7
1.79 - classcode (unused) __new__ funccode size class type attribute ...
1.80 - for C reference for reference reference
1.81 + 0 1 2 3 4 5 6
1.82 + classcode (unused) __new__ funccode size attribute ...
1.83 + for C reference for reference
1.84 instantiator
1.85
1.86 Instance of C:
1.87
1.88 - 0 1 2 3 4 5 6 7
1.89 - classcode attrcode C.__call__ funccode size class C attribute ...
1.90 - for C for C reference for reference reference
1.91 + 0 1 2 3 4 5 6
1.92 + classcode attrcode C.__call__ funccode size attribute ...
1.93 + for C for C reference for reference
1.94 (if exists) C.__call__
1.95
1.96 Function f:
1.97
1.98 - 0 1 2 3 4 5 6 7
1.99 - classcode attrcode code funccode size class attribute ...
1.100 - for for reference function (default)
1.101 - function function reference reference
1.102 + 0 1 2 3 4 5 6
1.103 + classcode attrcode code funccode size attribute ...
1.104 + for for reference (default)
1.105 + function function reference
1.106
1.107 Module m:
1.108
1.109 - 0 1 2 3 4 5 6 7
1.110 - classcode attrcode (unused) (unused) (unused) module type attribute ...
1.111 - for m for m reference (global)
1.112 - reference
1.113 + 0 1 2 3 4 5 6
1.114 + classcode attrcode (unused) (unused) (unused) attribute ...
1.115 + for m for m (global)
1.116 + reference
1.117
1.118 The __class__ Attribute
1.119 -----------------------
1.120
1.121 -All objects support the __class__ attribute and this is illustrated above with
1.122 -the first attribute.
1.123 +All objects should support the __class__ attribute, and in most cases this is
1.124 +done using the object table, yielding a common address for all instances of a
1.125 +given class.
1.126 +
1.127 +Function: refers to the function class
1.128 +Instance: refers to the class instantiated to make the object
1.129 +
1.130 +The object table cannot support two definitions simultaneously for both
1.131 +instances and their classes. Consequently, __class__ access on classes must be
1.132 +tested for and a special result returned.
1.133
1.134 Class: refers to the type class (type.__class__ also refers to the type class)
1.135 -Function: refers to the function class
1.136 -Instance: refers to the class instantiated to make the object
1.137 +
1.138 +For convenience, the first attribute of a class will be the common __class__
1.139 +attribute for all its instances. As noted above, direct access to this
1.140 +attribute will not be possible for classes, and a constant result will be
1.141 +returned instead.
1.142
1.143 Lists and Tuples
1.144 ----------------
1.145 @@ -539,8 +560,6 @@
1.146 resulting instance, along with locations for the attributes of the instance.
1.147 Since the instance header contains data common to all instances of a class, a
1.148 template header is copied to the start of the newly reserved memory region.
1.149 -The __class__ attribute is also an essential part of instances, and this is
1.150 -also copied to the new memory region.
1.151
1.152 Register Usage
1.153 ==============