# HG changeset patch # User Paul Boddie # Date 1298162115 -3600 # Node ID 7cc5058cbfb6d2be3308c6ad98079ca22a92e874 # Parent 5692f2b87aabb20840650d4f254b793a48be6987 Fixed __class__ to always be at instance attribute position 0, thus matching its class attribute position. Added notes about the __class__ attribute, its definition in the object table, and the copying of the attribute from instance templates upon instantiation. diff -r 5692f2b87aab -r 7cc5058cbfb6 docs/concepts.txt --- a/docs/concepts.txt Sun Feb 20 01:10:24 2011 +0100 +++ b/docs/concepts.txt Sun Feb 20 01:35:15 2011 +0100 @@ -192,7 +192,8 @@ A limitation of this representation is that instance attributes may not shadow class attributes: if an attribute with a given name is not defined on an instance, an attribute with the same name cannot be provided by the class of -the instance or any superclass of the instance's class. +the instance or any superclass of the instance's class. The exception to this +restriction is the __class__ attribute, as described below. The table can be compacted using a representation known as a displacement list (referred to as an object list in this context): @@ -240,9 +241,15 @@ each class and its instances. Since the table elements can only refer to a single absolute address, thus providing only a single value, such absolute references which are sufficient for most class attributes would not be -appropriate for the __class__ attribute. However, using an object-relative -location would require both classes and instances to retain an attribute -location specifically to hold the value appropriate for each object type. +appropriate for the __class__ attribute. Using a common object-relative +location of 0 permits the first attribute to be accessed via an object address +regardless of whether a class or instance is involved. + +Obviously, this requires both classes and instances to retain an attribute +location specifically to hold the value appropriate for each object type, +whereas a scheme which omits the __class__ attribute on classes would be able +to employ an absolute address in the table and maintain only a single address +to refer to the class for all instances. Comparing Tables as Matrices with Displacement Lists ---------------------------------------------------- @@ -532,6 +539,8 @@ resulting instance, along with locations for the attributes of the instance. Since the instance header contains data common to all instances of a class, a template header is copied to the start of the newly reserved memory region. +The __class__ attribute is also an essential part of instances, and this is +also copied to the new memory region. Register Usage ============== diff -r 5692f2b87aab -r 7cc5058cbfb6 micropython/data.py --- a/micropython/data.py Sun Feb 20 01:10:24 2011 +0100 +++ b/micropython/data.py Sun Feb 20 01:35:15 2011 +0100 @@ -1395,7 +1395,13 @@ # instance. for name in self.instattr: - instattr[name] = set() # position not yet defined + + # Special case: __class__ has to be at position 0. + + if name == "__class__": + instattr[name] = set([0]) + else: + instattr[name] = set() # position not yet defined reversed_bases = self.bases[:] reversed_bases.reverse()