Skip to content

Commit 874fb87

Browse files
author
Vimal
committed
* PEP8 corrections
1 parent e66a620 commit 874fb87

20 files changed

+50
-44
lines changed

02-encapsulation-2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
# NOTE: BREAKING ENCAPSULATION IS BAD.
1111

12+
1213
class MyClass(object):
1314
def set_val(self, val):
1415
self.value = val
@@ -22,8 +23,7 @@ def get_val(self):
2223
a.set_val(10)
2324
b.set_val(1000)
2425
a.value = 100 # <== Overriding `set_value` directly
25-
# <== ie.. Breaking encapsulation
26+
# <== ie.. Breaking encapsulation
2627

2728
a.get_val()
2829
b.get_val()
29-

03-encapsulation-3.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
# In this example, the code is written to enforce an intger as input, if we
1818
# don't break encapsulation and go through the gateway 'set_val()'
1919

20-
#
20+
#
21+
22+
2123
class MyInteger(object):
2224
def set_val(self, val):
2325
try:
@@ -26,7 +28,6 @@ def set_val(self, val):
2628
return
2729
self.val = val
2830

29-
3031
def get_val(self):
3132
print(self.val)
3233

@@ -49,10 +50,7 @@ def increment_val(self):
4950

5051
# Trying to break encapsulation in a new instance with a str
5152
b = MyInteger()
52-
b.val = "MyString" # <== Breaking encapsulation, works fine
53+
b.val = "MyString" # <== Breaking encapsulation, works fine
5354
b.get_val() # <== Prints the val set by breaking encap
5455
b.increment_val() # This will fail, since str + int wont work
5556
print("\n")
56-
57-
58-

04-init_constructor-1.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# the method is private. It's called private or magic methods
1313
# since it's called internally and automatically.
1414

15+
1516
class MyNum(object):
1617
def __init__(self):
1718
print("Calling the __init__() constructor!\n")
@@ -24,6 +25,3 @@ def increment(self):
2425
dd = MyNum()
2526
dd.increment() # will print 1
2627
dd.increment() # will print 2
27-
28-
29-

05-init_constructor-2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
# We add a test in the __init__() constructor to check
66
# if 'value' is an int or not.
7+
8+
79
class MyNum(object):
810
def __init__(self, value):
911
try:
@@ -19,4 +21,4 @@ def increment(self):
1921

2022
a = MyNum(10)
2123
a.increment() # This should print 11
22-
a.increment() # This should print 12
24+
a.increment() # This should print 12

07-class-attributes-2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# present in the instance, and hence the lookup goes a level above and gets it from
1717
# the class.
1818

19+
1920
class YourClass(object):
2021
classy = "class value"
2122

@@ -25,8 +26,7 @@ class YourClass(object):
2526
dd.classy = "Instance value"
2627
print(dd.classy) # This should return the string "Instance value"
2728

28-
del dd.classy # This will delete the value set for 'dd.classy' in the instance.
29-
print(dd.classy) # Since the overriding attribute was deleted, this will print 'class value'.
30-
31-
32-
29+
# This will delete the value set for 'dd.classy' in the instance.
30+
del dd.classy
31+
# Since the overriding attribute was deleted, this will print 'class value'.
32+
print(dd.classy)

08-class-instance-attributes-1.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# it each time we create an instance. This can help count the
1010
# number of instances at the time of instantiation.
1111

12+
1213
class InstanceCounter(object):
1314
count = 0
1415

@@ -32,5 +33,3 @@ def get_count(self):
3233
for obj in (a, b, c):
3334
print("value of obj: %s" % obj.get_val())
3435
print("Count : %s" % obj.get_count())
35-
36-

09-inheritance-1.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@
1212
# Hence, any instances created from the class `Time` can access
1313
# the methods defined in the parent class `Date`.
1414

15+
1516
class Date(object):
1617
def get_date(self):
1718
print("2016-05-14")
1819

20+
1921
class Time(Date):
2022
def get_time(self):
2123
print("07:00:00")
2224

2325
# Creating an instance from `Date`
2426
dt = Date()
25-
dt.get_date() #Accesing the `get_date()` method of `Date`
27+
dt.get_date() # Accesing the `get_date()` method of `Date`
2628
print("--------")
2729

2830
# Creating an instance from `Time`.
2931
tm = Time()
3032
tm.get_time() # Accessing the `get_time()` method from `Time`.
3133
# Accessing the `get_date() which is defined in the parent class `Date`.
32-
tm.get_date()
34+
tm.get_date()

11-polymorphism-1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# the instance will only pick up the first hit,
2020
# ie.. from the 'class' and won't go to the parent class.
2121

22+
2223
class Animal(object):
2324

2425
def __init__(self, name):
@@ -36,6 +37,7 @@ def fetch(self, thing):
3637
def show_affection(self):
3738
print('{0} wags tail'.format(self.name))
3839

40+
3941
class Cat(Animal):
4042

4143
def swatstring(self):
@@ -46,4 +48,3 @@ def show_affection(self):
4648

4749
for a in (Dog('Rover'), Cat('Fluffy'), Cat('Lucky'), Dog('Scout')):
4850
a.show_affection()
49-

12-polymorphism-2.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,4 @@
2424
print(len(text))
2525

2626
print(len("Hello"))
27-
print(len({'a':1, 'b': 2, 'c' : 3}))
28-
29-
30-
31-
27+
print(len({'a': 1, 'b': 2, 'c': 3}))

14-multiple-inheritance-1.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,27 @@
2222

2323
# The method lookup flow in this case is : D -> B -> A -> C
2424

25+
2526
class A(object):
2627

2728
def dothis(self):
2829
print("doing this in A")
2930

31+
3032
class B(A):
3133
pass
3234

35+
3336
class C(object):
3437
def dothis(self):
3538
print("doing this in C")
3639

40+
3741
class D(B, C):
3842
pass
3943

4044
d_instance = D()
4145
d_instance.dothis() # <== This should print from class A.
4246

4347
print("\nPrint the Method Resolution Order")
44-
print(D.mro())
48+
print(D.mro())

0 commit comments

Comments
 (0)