1.1 --- a/docs/concepts.txt Sun Feb 20 01:10:24 2011 +0100
1.2 +++ b/docs/concepts.txt Sun Feb 20 01:35:15 2011 +0100
1.3 @@ -192,7 +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.
1.8 +the instance or any superclass of the instance's class. The exception to this
1.9 +restriction is the __class__ attribute, as described below.
1.10
1.11 The table can be compacted using a representation known as a displacement
1.12 list (referred to as an object list in this context):
1.13 @@ -240,9 +241,15 @@
1.14 each class and its instances. Since the table elements can only refer to a
1.15 single absolute address, thus providing only a single value, such absolute
1.16 references which are sufficient for most class attributes would not be
1.17 -appropriate for the __class__ attribute. However, using an object-relative
1.18 -location would require both classes and instances to retain an attribute
1.19 -location specifically to hold the value appropriate for each object type.
1.20 +appropriate for the __class__ attribute. Using a common object-relative
1.21 +location of 0 permits the first attribute to be accessed via an object address
1.22 +regardless of whether a class or instance is involved.
1.23 +
1.24 +Obviously, this requires both classes and instances to retain an attribute
1.25 +location specifically to hold the value appropriate for each object type,
1.26 +whereas a scheme which omits the __class__ attribute on classes would be able
1.27 +to employ an absolute address in the table and maintain only a single address
1.28 +to refer to the class for all instances.
1.29
1.30 Comparing Tables as Matrices with Displacement Lists
1.31 ----------------------------------------------------
1.32 @@ -532,6 +539,8 @@
1.33 resulting instance, along with locations for the attributes of the instance.
1.34 Since the instance header contains data common to all instances of a class, a
1.35 template header is copied to the start of the newly reserved memory region.
1.36 +The __class__ attribute is also an essential part of instances, and this is
1.37 +also copied to the new memory region.
1.38
1.39 Register Usage
1.40 ==============
2.1 --- a/micropython/data.py Sun Feb 20 01:10:24 2011 +0100
2.2 +++ b/micropython/data.py Sun Feb 20 01:35:15 2011 +0100
2.3 @@ -1395,7 +1395,13 @@
2.4 # instance.
2.5
2.6 for name in self.instattr:
2.7 - instattr[name] = set() # position not yet defined
2.8 +
2.9 + # Special case: __class__ has to be at position 0.
2.10 +
2.11 + if name == "__class__":
2.12 + instattr[name] = set([0])
2.13 + else:
2.14 + instattr[name] = set() # position not yet defined
2.15
2.16 reversed_bases = self.bases[:]
2.17 reversed_bases.reverse()