Skip to content

Commit 8c9cd8c

Browse files
committed
completing Inheritance and Polymorphism - section4 python beyond the basics OOP
1 parent 854d3cc commit 8c9cd8c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Composition versus Inheritance
2+
# Python beyonds the basics - Object oriented programming
3+
4+
"""
5+
Inheritance can be brittle(a change may require changes
6+
elsewhere)
7+
Decoupled code is classes, functions, etc. that work
8+
independently and don't depend on one another.
9+
As long as the interface is maintained, interactions between
10+
classes will work.
11+
Not checking or requiring particular types in polymorphic and
12+
pythonic
13+
"""
14+
15+
import random
16+
import StringIO
17+
18+
class WriteMyStuff(object):
19+
20+
def __init__(self,writer):
21+
self.writer = writer
22+
23+
def write(self):
24+
write_text = "This is a message"
25+
self.writer.write(write_text)
26+
27+
28+
fh = open('test.txt', 'w')
29+
w1 = WriteMyStuff(fh)
30+
w1.write()
31+
fh.close()
32+
33+
34+
sioh = StringIO.StringIO()
35+
w2 = WriteMyStuff(sioh)
36+
37+
w2.write()

Inheritance and Polymorphism/method_overloading_extending_and_providing.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Method Overloading - Extending and Providing
22
# Python beyond the basics - object oriented programming
3+
"""
4+
When working in a child class we can choose to implement parent
5+
class methods in different ways
6+
Inherit : simply use the parent class defined methods
7+
Override/overload : provide childs own version of a method
8+
Extend : do work in addition to that in parent's methods
9+
provide : implement abstract method that parent requires
10+
"""
311

412
import abc
513

0 commit comments

Comments
 (0)