1- Summary
2- =======
1+ Coherent example
2+ ================
33
4- The points made so far, are the basics of using classes and objects in Python. I
5- will now summarise these basics in a single example:
4+ The points mentioned so far are the basics of using classes and objects in
5+ Python. I will now illustrate these basics in a coherent example:
6+ :download: `form.py `.
67
78#. First, we create a base class:
89
910 .. literalinclude :: form.py
1011 :language: python
1112 :linenos:
12- :lines: 4-13
13- :lineno-start: 4
13+ :lines: 1-12
14+ :lineno-start: 1
1415
1516 Line 7
1617 The ``__init__ `` method requires one instance (``self ``) and two
1718 parameters.
1819 Lines 8 and 9
1920 The two instance variables ``x `` and ``y ``, which are accessed via
2021 ``self ``.
21- Line 11
22+ Line 10
2223 The ``move `` method requires one instance (``self ``) and two parameters.
23- Lines 12 and 13
24+ Lines 11 and 12
2425 Instance variables that are set in the ``move `` method.
2526
2627#. Next, create a subclass that inherits from the base class ``Form ``:
@@ -37,26 +38,26 @@ will now summarise these basics in a single example:
3738 ``Square ``’s ``__init__ `` takes one instance (``self ``) and three
3839 parameters, all with defaults.
3940 Line 20
40- ``Circle ``’s ``__init__ `` uses ``super() `` to call ``Form ``’s
41+ ``Square ``’s ``__init__ `` uses ``super() `` to call ``Form ``’s
4142 ``__init__ ``.
4243
4344#. Finally, we create another subclass that also contains a static method:
4445
4546 .. literalinclude :: form.py
4647 :language: python
4748 :linenos:
48- :lines: 27-
49+ :lines: 27-43
4950 :lineno-start: 27
5051
51- Lines 30 and 31
52+ Lines 29 and 30
5253 ``pi `` and ``circles `` are class variables for ``Circle ``.
53- Line 33
54+ Line 34
5455 In the ``__init__ `` method, the instance inserts itself into the
5556 ``circles `` list.
56- Lines 38 and 39
57+ Lines 37 and 38
5758 ``circumferences `` is a class method and takes the class itself
5859 (``cls ``) as a parameter.
59- Line 42
60+ Line 41
6061 uses the parameter ``cls `` to access the class variable ``circles ``.
6162
6263Now you can create some instances of the class ``Circle `` and analyse them.
0 commit comments