44What are classes?
55-----------------
66
7+ .. figure :: classes.png
8+
79Classes are a tool to manage complexity in a program. They group two
810things in a single structural unit: **attributes (data) ** and **methods
911(behavior) **.
@@ -13,8 +15,7 @@ so that your main program becomes simple. In my opinion, this way of
1315structuring code should be the main motivation to using classes in
1416Python.
1517
16- In this article, you find an example how to use a class to structure
17- your code.
18+ In this chapter, you find an example how to use a class to structure your code.
1819
1920--------------
2021
@@ -27,116 +28,83 @@ To define a class, you need to define three things:
2728- define attributes (variables that belong to the class)
2829- define methods (functions that belong to the class)
2930
30- In the code below, a class for a bank account is defined:
31-
32- .. code :: python3
33-
34- class Account:
35- """
36- Account of a bank client.
37- """
38- def __init__(self, owner, start_balance=0):
39- self.name = owner
40- self.balance = start_balance
41-
42- def deposit(self, amt):
43- self.balance += amt
31+ In the code below, a class for a **Planet ** is defined:
4432
45- def withdraw(self, amt):
46- self.balance -= amt
33+ .. literalinclude :: planet.py
4734
48- The class ``Account `` contains two attributes (`` name `` and `` balance ``)
49- and two methods (`` deposit `` and `` withdraw ``) .
35+ The class ``Planet `` contains three attributes
36+ and two methods.
5037
5138Note that you need to add the word ``self `` every time you refer to an
5239attribute. You also must use ``self `` as the first parameter in every
5340method of a class.
5441
55- --------------
56-
5742Creating Objects
5843----------------
5944
6045To use a class, you need to create an object from it first. Objects are
6146*“live versions” * of a class, the class being an idealized abstration
6247(in the sense of `Platos Theory of Forms <https://en.wikipedia.org/wiki/Theory_of_forms >`__).
63- If you think of **BankAccount ** as a class, the actual accounts of ** Ada Lovelace ** and **Mahatma Gandi **
48+ If you think of **Planet ** as a class, the actual planets ** Earth ** and **Pandalor **
6449would be the objects of that class.
6550
6651You can create multiple objects from a class, and each objects has its
67- own, independent attributes (e.g. if **BankAccount ** has an attribute **balance **,
68- then **Ada ** and **Mahatma ** could have a different amount of money).
69-
70- Syntactically, you can think of a class as a function that returns
52+ own, independent attributes. Syntactically, you can think of a class as a function that returns
7153objects. (This is a gross oversimplification to what textbooks on
7254classes say, but in Python it is more or less what happens).
7355
74- To create ``Account `` objects, you need to call the class. Creating an
56+ To create ``Planet `` objects, you need to call the class. Creating an
7557object will automatically call the constructor ``__init__(self) `` with
7658the parameters supplied.
7759
7860.. code :: python3
7961
80- a = Account('Ada Lovelace', 1234)
81- m = Account('Mahatma Gandhi', 10)
62+ earth = Planet(name="Earth", description="the blue planet")
63+ pandalor = Planet(name="Pandalor", description="home of the space pandas")
64+ arcturus = Planet(name="Arcturus", description="an icy planet, home of penguins")
65+
8266
8367 Then you can access the attributes like any variable using the dot (``. ``) syntax:
8468
8569.. code :: python3
8670
87- print(a.name)
88- print(m.balance)
71+ print(earth.name)
72+ print(earth.balance)
73+
74+ Exercise: Methods
75+ -----------------
8976
9077And you can call methods in a similar way:
9178
9279.. code :: python3
9380
94- a.deposit(100)
95- a.withdraw(10)
96- print(a.balance)
81+ earth.add_connection(pandalor)
82+ earth.add_connection(arcturus)
9783
98- Note that these methods modify the state of *Adas * account object, but
99- not *Mahatmas *.
84+ earth.show_connections()
10085
101- --------------
102-
103- Making classes printable
104- ------------------------
105-
106- One disadvantage of classes is that when you print an object, you will
107- see something like this:
108-
109- ::
110-
111- <__main__.Account at 0x7f64519d8438>
86+ Note that these methods modify the state of the planet *Earth *, but not the other two.
11287
113- A good workaround is to add a special method, ``__repr__(self) `` to the
114- class that returns a string. This method will be called every time a
115- string representation is needed: when printing and object, when an
116- object appears inside a list or in error messages.
88+ Implement the `show_connections() ` method using the code from the program `space_game.py `.
11789
118- Typically, you would build a short string in `` __repr__(self) `` that
119- describes the object:
90+ Exercise: Refactor using classes
91+ --------------------------------
12092
121- .. code :: python3
122-
123- def __repr__(self):
124- return f"<Account of '{self.name}' with {self.balance} galactic credits>"
125-
126- With this method defined, the instruction
127-
128- .. code :: python3
93+ Simplify `space_game.py ` using the `Planet ` class.
12994
130- print(a)
13195
132- would result in the output
96+ Four Ways to create classes
97+ ---------------------------
13398
134- ::
99+ Python knows multiple flavors of defining and using classes.
100+ These are recent developments (~2018+), strongly relying on the availability of **Type Hints **.
135101
136- <Account of 'Ada Lovelace' with 1324 galactic credits>"
102+ Execute and examine the following code examples, defining a level for a point-eating game:
137103
138- It is a good idea to implement ``__repr__(self) `` as the first method in
139- a new class.
104+ - :download: `class_vanilla.py `
105+ - :download: `class_typedict.py `
106+ - :download: `class_dataclass.py `
107+ - :download: `class_pydantic.py `
140108
141109--------------
142110
@@ -151,9 +119,9 @@ the same purpose equally well.
151119
152120Another motivation for using classes you find in textbooks is
153121**encapsulation **, isolating parts of your program from the rest.
154- Encapsulation does not exist in Python (e.g. you cannot declare parts of
122+ Encapsulation does not exist in Python (e.g. you cannot declare parts of
155123a class as ``private `` in a way that cannot be circumvented). If you
156- depend on your code being strictly isolated from other parts (e.g. in a
124+ depend on your code being strictly isolated from other parts (e.g. in a
157125security-critical application or when organizing a very large program),
158126**consider other programming languages than Python. **
159127
0 commit comments