You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Python only ever has one constructor that is called `__init__` and must have the `self` parameter
85
-
- Python has no method overloading
86
-
- We don't explicitly define the class properties `first_name` and `last_name`
87
-
- You create them on-the-fly in the constructor
88
-
- What makes a function a method in Python is only governed by the explicit `self` parameter
89
-
- And you always have to use `self.` if you want to access class properties
90
-
- A static method in Python needs the `@staticmethod` annotation and omits the `self` parameter
91
-
- A static method just means that it won't access any class properties
92
-
- Creating a new object works similar in Python, but we omit the `new` keyword
93
-
- There are no access modifiers and there also is no `final` in Python
87
+
#### Constructors
94
88
95
-
---
89
+
- There is only one **constructor** that is called `__init__` with a mandatory `self` parameter
90
+
- Python does not support constructor overloading or **method overloading**
91
+
- We don't explicitly define the **class properties**`first_name` and `last_name`,
92
+
but you create them on-the-fly in the constructor or elsewhere
93
+
94
+
#### Functions and methods
95
+
96
+
- What makes a function a method is only governed by the explicit `self` parameter
97
+
- You must always use `self.` if you want to access class properties
98
+
- There are no access modifiers and there is also no `final` in Python
99
+
100
+
#### Static methods
101
+
102
+
- A **static method** needs the `@staticmethod` annotation and omits the `self` parameter
103
+
- A static method just means that it won't access any class properties
96
104
97
-
##Interfaces
105
+
#### Instantiation
98
106
99
-
What you might know from interfaces in Java can be achieved a little bit different with a dynamically-typed language like Python.
107
+
- Creating a new object works similar, but you omit the `new` keyword
108
+
- The `main` method in Python is actually a condition - we will learn more in the next modules
109
+
110
+
---
111
+
112
+
## Informal Interfaces
100
113
101
114
<divclass="sidebyside">
102
115
@@ -138,10 +151,7 @@ class Square implements Shape {
138
151
classShapesProgram {
139
152
140
153
publicstaticvoidmain(String[] args) {
141
-
Square square =newSquare(10);
142
-
Circle circle =newCircle(5);
143
-
144
-
Shape[] shapes = {square, circle};
154
+
Shape[] shapes = { newCircle(5), newSquare(10) };
145
155
for (Shape shape : shapes) {
146
156
System.out.println(shape.area());
147
157
}
@@ -150,23 +160,30 @@ class ShapesProgram {
150
160
151
161
```
152
162
153
-
```py
163
+
```py [|3-5|8-13|16-21|24-26|]
154
164
# ./python/m09_shapes_program.py
155
165
156
-
classCircle:
166
+
classShape:
167
+
defarea(self):
168
+
pass
169
+
170
+
171
+
classCircle(Shape):
157
172
def__init__(self, radius):
158
173
self.radius = radius
159
174
160
175
defarea(self):
161
176
return3.14159*self.radius *self.radius
162
177
163
-
classSquare:
178
+
179
+
classSquare(Shape):
164
180
def__init__(self, length):
165
181
self.length = length
166
182
167
183
defarea(self):
168
184
returnself.length *self.length
169
185
186
+
170
187
shapes = [Circle(5), Square(10)]
171
188
for shape in shapes:
172
189
print(shape.area())
@@ -175,11 +192,47 @@ for shape in shapes:
175
192
176
193
</div>
177
194
195
+
### What do we notice here?
196
+
197
+
- There is no `interface` type in Python, instead we use a normal `class` and inhert from it
198
+
- Methods are overridden without an explicit annotation - they are always replaced
199
+
- No type checks are enforced by Python, try removing `Shape` or renaming the `area()` method ...
200
+
178
201
---
179
202
180
-
### What do we notice here?
203
+
## Duck-Typing
204
+
205
+
Since there is NO type-checking at compile-time, we may also solely rely on [**duck-typing**](https://docs.python.org/3/glossary.html#term-duck-typing).
206
+
An `AttributeError` is thrown at runtime, if the method you are calling wouldn't exist. *
207
+
208
+
```py [|3-16|19-21|21|]
209
+
# ./python/m09_shapes_duck_typing.py
210
+
211
+
classCircle:
212
+
def__init__(self, radius):
213
+
self.radius = radius
214
+
215
+
defarea(self):
216
+
return3.14159*self.radius *self.radius
217
+
218
+
219
+
classSquare:
220
+
def__init__(self, length):
221
+
self.length = length
222
+
223
+
defarea(self):
224
+
returnself.length *self.length
225
+
226
+
227
+
shapes = [Circle(5), Square(10)]
228
+
for shape in shapes:
229
+
print(shape.area())
230
+
231
+
```
232
+
233
+
### But, how can I have a "real" interfaces and inheritance in Python?
234
+
235
+
- Use abstract classes via the [**`abc` module**](https://docs.python.org/3/library/abc.html)
236
+
- Learn more about formal interfaces at [realpython.com/python-interface](https://realpython.com/python-interface/)
181
237
182
-
- Because there is no hard type checking at compile-time, we can rely on duck-typing here
183
-
- However, for a better code structures, Python offers the following
184
-
- abstract classes (see https://docs.python.org/3/library/abc.html)
185
-
- protocols (see https://www.python.org/dev/peps/pep-0544/)
238
+
<small>* Look into [**type hints**](https://docs.python.org/3/library/typing.html) to have better type-checking at runtime and even before you run your code.</small>
0 commit comments