Skip to content

Commit 744d739

Browse files
committed
updated syllabus
1 parent e84cb65 commit 744d739

File tree

6 files changed

+234
-54
lines changed

6 files changed

+234
-54
lines changed

Examples/week-06-OO/even_int.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
1010
"""
1111

12-
class EvenInt():
12+
13+
class EvenInt(int):
1314
"""
1415
An integer that is always even
1516
"""
16-
pass
1717

18+
def __new__(cls, input):
19+
input = float(input)
20+
i = int(round(input / 2) * 2)
21+
return int.__new__(cls, i)

Examples/week-06-OO/mixins.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import time
22

33

4-
class LoggingMixin(object):
4+
class Base(object):
55
def log(self):
6-
print "%s: %s" % (time.time(), repr(self))
6+
pass
77

88

9-
class Vehicle(object):
9+
class LoggingMixin(Base):
1010
def log(self):
11-
print self.__repr__()
11+
print "%s" % time.time(),
12+
print "self is:", self
13+
super(LoggingMixin, self).log()
14+
1215

16+
class Vehicle(Base):
17+
def log(self):
18+
print self.__repr__()
19+
super(Vehicle, self).log()
1320

1421
class TwoWheeledVehicle(Vehicle):
1522
pass
@@ -23,7 +30,7 @@ class HeavyVehicle(Vehicle):
2330
pass
2431

2532

26-
class Bike(TwoWheeledVehicle, LightVehicle):
33+
class Bike(LoggingMixin, TwoWheeledVehicle, LightVehicle):
2734
pass
2835

2936

Examples/week-06-OO/new_example.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,36 @@
99
## the general case:
1010
class Test(object):
1111
def __new__(cls, arg):
12-
print "in __new__",
12+
print "in __new__",
1313
print arg
1414
obj = object.__new__(cls, arg)
1515
obj.this = arg # but you probably don't want to do this!
1616
return obj
1717

1818
def __init__(self, arg):
19-
print "in __init__",
19+
print "in __init__",
2020
print arg
2121
self.that = arg
2222

2323

2424
##subclassing a string
2525

26+
class Test(object):
27+
def __init__(self):
28+
print "in Test.__init__"
29+
2630
class CapitalString(str):
2731
"""
2832
A string class that is always capitalized...
2933
"""
3034
def __new__(cls, in_string):
3135
print "in CapitalString.__new__"
32-
return str.__new__(cls, in_string.title() )
36+
return Test.__new__(cls, in_string)
37+
#return str.__new__(cls, in_string.title() )
3338

3439
# try it:
35-
if __name__ == "__main__":
36-
print CapitalString("this is a string")
40+
#if __name__ == "__main__":
41+
# print CapitalString("this is a string")
3742

3843

3944

0 commit comments

Comments
 (0)