44 ``Object `` and ``PyType `` Basic Patterns
55****************************************
66
7-
8- .. note :: Check for ``PyBaseObject``
9-
10-
11-
127In this section we set out the plain Java object approach
138to representing Python objects.
149We did this already in ``rt3 ``,
@@ -54,8 +49,8 @@ or possibly a cascade of inter-dependent ``PyType``\s.
5449We must guard against the possibility that
5550some other thread may already be doing overlapping work.
5651
57- Types of Type
58- -------------
52+ Classes of Type
53+ ---------------
5954
6055Type objects fall into three categories,
6156which have distinct Java implementations:
@@ -99,10 +94,13 @@ Primary Representation
9994The *primary * representation class is a (least derived) Java class,
10095instances of which are acceptable as
10196the ``self `` argument to methods of the type.
97+ (Notice we use the term *representation * in two ways:
98+ for the Java representation class, and
99+ for the ``Representation `` object.)
102100
103101In the case of a **simple type **,
104102all representation classes for the type have a common root class,
105- which is the primary representation.
103+ which is the primary representation class .
106104For example, all three classes of type object extend ``PyType ``,
107105the primary representation of ``type ``.
108106
@@ -118,12 +116,13 @@ Canonical Base Class
118116Secondly, we need the idea of a *canonical base * class.
119117This is the Java class on which the implementation of
120118a subclass defined in Python will be based.
121- Specific Java classes may have to be defined
119+ Specific Java classes are needed
122120as representations of the Python subclasses,
123121but all will be subclasses in Java of the canonical base.
124122
125123Instances of subclasses of the canonical base
126124must be acceptable as a ``self `` argument to methods.
125+ The reason for this is in the definition of instance methods.
127126When a Python subclass,
128127seeking a method along the MRO finds it in a type object,
129128it will call the implementation matching the primary representation.
@@ -149,8 +148,22 @@ as they are mostly ``final``.
149148As so often, ``object `` is an exception,
150149where the canonical class is ``java.lang.Object ``.
151150
151+ A Java subclass of the canonical base of a type is either
152+ treated as representing the same type
153+ (by being mapped to the same ``Representation ``), or
154+ the representation of a Python subclass of the type.
155+ The choice is made when we register the Java class to either
156+ the existing ``Representation `` or
157+ create a new representation for the Python subclass.
158+ When the type in question is a replaceable type,
159+ since equivalent types share the same canonical base,
160+ the Java subclass maps either
161+ to the ``SharedRepresentation `` of the type(s),or
162+ to new ``SharedRepresentation `` defining an equivalence.
163+
152164.. note ::
153- Not sure about the detail of this.
165+ The detail of this is gradually being confirmed
166+ in the implementation of Python exceptions and sublasses.
154167 The concept is right,
155168 but does the canonical base have properties that make it
156169 always a "designer" class?
@@ -249,7 +262,7 @@ is itself the ``PyType``.
249262
250263 abstract class Representation {
251264 pythonType(o)
252- javaType ()
265+ javaClass ()
253266 }
254267
255268 abstract class PyType {
@@ -317,23 +330,24 @@ cites the same ``Representation``.
317330
318331 abstract class Representation {
319332 pythonType(o)
320- javaType ()
333+ javaClass ()
321334 }
322335
323336 abstract class PyType {
324337 getDict()
325338 lookup(attr)
326339 }
327340
328- interface WithType {
341+ interface WithClassAssignment {
329342 getType()
343+ setType(t)
330344 }
331345
332- T .up.|> WithType
346+ T .up.|> WithClassAssignment
333347 T --> ReplaceableType
334348
335349 SharedRepresentation -up-|> Representation
336- SharedRepresentation "1" -- "*" ReplaceableType
350+ SharedRepresentation "1" < -- "*" ReplaceableType
337351
338352 ' Representation <|-- PyType
339353 PyType -- |> Representation
@@ -344,7 +358,7 @@ cites the same ``Representation``.
344358Instances of a class defined in Python
345359(by a ``class `` statement),
346360that have no built-in types in their MRO but ``object ``,
347- will have the Java class ``PyBaseObject `` for ``T ``.
361+ will have the Java class ``PyObject `` for ``T ``.
348362In general, ``T `` will be a Java *extension point * subclass of
349363the representation of the most-derived common ancestor.
350364(The case of mutiple Java bases needs investigation.)
@@ -354,7 +368,8 @@ and what ``__class__`` assignments are allowed.
354368We observe that in CPython,
355369acceptable values for ``__class__ ``
356370define an equivalence relation amongst Python classes.
357- Let :math: `R(A,B)` be the statement ``A.__class__ = B.__class__ `` is allowed.
371+ Let :math: `R(A,B)` be the statement that
372+ ``A.__class__ = B.__class__ `` is allowed.
358373Then :math: `R(A,A)`,
359374:math: `R(A,B) ⇒ R(B,A)`,
360375and :math: `R(A,B) ∧ R(B,C) ⇒ R(A,C)`.
@@ -419,7 +434,7 @@ leading to Python type ``type``.
419434
420435 abstract class Representation {
421436 pythonType(o)
422- javaType ()
437+ javaClass ()
423438 }
424439
425440 abstract class PyType {
0 commit comments