@@ -6,94 +6,94 @@ will now summarise these basics in a single example:
66
77#. First, we create a base class:
88
9- .. literalinclude :: form.py
10- :language: python
11- :linenos:
12- :lines: 4-13
13- :lineno-start: 4
14-
15- Line 7
16- The ``__init__ `` method requires one instance (``self ``) and two
17- parameters.
18- Lines 8 and 9
19- The two instance variables ``x `` and ``y ``, which are accessed via
20- ``self ``.
21- Line 11
22- The ``move `` method requires one instance (``self ``) and two parameters.
23- Lines 12 and 13
24- Instance variables that are set in the ``move `` method.
9+ .. literalinclude :: form.py
10+ :language: python
11+ :linenos:
12+ :lines: 4-13
13+ :lineno-start: 4
14+
15+ Line 7
16+ The ``__init__ `` method requires one instance (``self ``) and two
17+ parameters.
18+ Lines 8 and 9
19+ The two instance variables ``x `` and ``y ``, which are accessed via
20+ ``self ``.
21+ Line 11
22+ The ``move `` method requires one instance (``self ``) and two parameters.
23+ Lines 12 and 13
24+ Instance variables that are set in the ``move `` method.
2525
2626#. Next, create a subclass that inherits from the base class ``Form ``:
2727
28- .. literalinclude :: form.py
29- :language: python
30- :linenos:
31- :lines: 16-21
32- :lineno-start: 16
33-
34- Line 16
35- The class ``Square `` inherits from the class ``Form ``.
36- Line 19
37- ``Square ``’s ``__init__ `` takes one instance (``self ``) and three
38- parameters, all with defaults.
39- Line 20
40- ``Circle ``’s ``__init__ `` uses ``super() `` to call ``Form ``’s
41- ``__init__ ``.
28+ .. literalinclude :: form.py
29+ :language: python
30+ :linenos:
31+ :lines: 16-21
32+ :lineno-start: 16
33+
34+ Line 16
35+ The class ``Square `` inherits from the class ``Form ``.
36+ Line 19
37+ ``Square ``’s ``__init__ `` takes one instance (``self ``) and three
38+ parameters, all with defaults.
39+ Line 20
40+ ``Circle ``’s ``__init__ `` uses ``super() `` to call ``Form ``’s
41+ ``__init__ ``.
4242
4343#. Finally, we create another subclass that also contains a static method:
4444
45- .. literalinclude :: form.py
46- :language: python
47- :linenos:
48- :lines: 27-
49- :lineno-start: 27
50-
51- Lines 30 and 31
52- ``pi `` and ``circles `` are class variables for ``Circle ``.
53- Line 33
54- In the ``__init__ `` method, the instance inserts itself into the
55- ``circles `` list.
56- Lines 38 and 39
57- ``circumferences `` is a class method and takes the class itself
58- (``cls ``) as a parameter.
59- Line 42
60- uses the parameter ``cls `` to access the class variable ``circles ``.
45+ .. literalinclude :: form.py
46+ :language: python
47+ :linenos:
48+ :lines: 27-
49+ :lineno-start: 27
50+
51+ Lines 30 and 31
52+ ``pi `` and ``circles `` are class variables for ``Circle ``.
53+ Line 33
54+ In the ``__init__ `` method, the instance inserts itself into the
55+ ``circles `` list.
56+ Lines 38 and 39
57+ ``circumferences `` is a class method and takes the class itself
58+ (``cls ``) as a parameter.
59+ Line 42
60+ uses the parameter ``cls `` to access the class variable ``circles ``.
6161
6262Now you can create some instances of the class ``Circle `` and analyse them.
6363Since the ``__init__ `` method of ``Circle `` has default parameters, you can
6464create a circle without specifying any parameters:
6565
66- .. code-block :: pycon
66+ .. code-block :: pycon
6767
68- >>> import form
69- >>> c1 = form.Circle()
70- >>> c1.diameter, c1.x, c1.y
71- (1, 0, 0)
68+ >>> import form
69+ >>> c1 = form.Circle()
70+ >>> c1.diameter, c1.x, c1.y
71+ (1, 0, 0)
7272
7373 If you specify parameters, they are used to set the values of the instance:
7474
75- .. code-block :: pycon
75+ .. code-block :: pycon
7676
77- >>> c2 = form.Circle(2, 3, 4)
78- >>> c2.diameter, c2.x, c2.y
79- (2, 3, 4)
77+ >>> c2 = form.Circle(2, 3, 4)
78+ >>> c2.diameter, c2.x, c2.y
79+ (2, 3, 4)
8080
8181 When you call the ``move() `` method, Python does not find a ``move() `` method in
8282the ``Circle `` class, so it goes up the inheritance hierarchy and uses the
8383``move() `` method of ``Form ``:
8484
85- .. code-block :: pycon
85+ .. code-block :: pycon
8686
87- >>> c2.move(5, 6)
88- >>> c2.diameter, c2.x, c2.y
89- (2, 8, 10)
87+ >>> c2.move(5, 6)
88+ >>> c2.diameter, c2.x, c2.y
89+ (2, 8, 10)
9090
9191 You can also call the class method ``circumferences() `` of the class ``Circle ``,
9292either through the class itself or through an instance:
9393
94- .. code-block :: pycon
94+ .. code-block :: pycon
9595
96- >>> form.Circle.circumferences()
97- 9.424769999999999
98- >>> c2.circumferences()
99- 9.424769999999999
96+ >>> form.Circle.circumferences()
97+ 9.424769999999999
98+ >>> c2.circumferences()
99+ 9.424769999999999
0 commit comments