Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita5424a8

Browse files
committed
Format with Black
1 parentafe5dab commita5424a8

File tree

39 files changed

+91
-60
lines changed

39 files changed

+91
-60
lines changed

‎01-encapsulation-1.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212

1313

1414
classMyClass(object):
15-
1615
defset_val(self,val):
1716
self.value=val
1817

1918
defget_val(self):
20-
#print(self.value)
19+
#print(self.value)
2120
returnself.value
2221

22+
2323
a=MyClass()
2424
b=MyClass()
2525

‎02-encapsulation-2.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ def set_val(self, val):
1717
defget_val(self):
1818
print(self.value)
1919

20+
2021
a=MyClass()
2122
b=MyClass()
2223

2324
a.set_val(10)
2425
b.set_val(1000)
25-
a.value=100# <== Overriding `set_value` directly
26+
a.value=100# <== Overriding `set_value` directly
2627
# <== ie.. Breaking encapsulation
2728

2829
a.get_val()

‎03-encapsulation-3.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def increment_val(self):
3535
self.val=self.val+1
3636
print(self.val)
3737

38+
3839
a=MyInteger()
3940
a.set_val(10)
4041
a.get_val()
@@ -51,6 +52,6 @@ def increment_val(self):
5152
# Trying to break encapsulation in a new instance with a str
5253
b=MyInteger()
5354
b.val="MyString"# <== Breaking encapsulation, works fine
54-
b.get_val()# <== Prints the val set by breaking encap
55+
b.get_val()# <== Prints the val set by breaking encap
5556
b.increment_val()# This will fail, since str + int wont work
5657
print("\n")

‎04-init_constructor-1.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def increment(self):
2222
self.val=self.val+1
2323
print(self.val)
2424

25+
2526
dd=MyNum()
2627
dd.increment()# will print 1
2728
dd.increment()# will print 2

‎05-init_constructor-2.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ def increment(self):
2020

2121

2222
a=MyNum(10)
23-
a.increment()# This should print 11
24-
a.increment()# This should print 12
23+
a.increment()# This should print 11
24+
a.increment()# This should print 12

‎06-class-attributes-1.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ class YourClass(object):
1515
defset_val(self):
1616
self.insty=100
1717

18+
1819
dd=YourClass()
19-
dd.classy# This will fetch the class attribute 10.
20+
dd.classy# This will fetch the class attribute 10.
2021
dd.set_val()
21-
dd.insty# This will fetch the instance attribute 100.
22+
dd.insty# This will fetch the instance attribute 100.
2223

2324
# Once `dd` is instantiated, we can access both the class and instance
2425
# attributes, ie.. dd.classy and dd.insty.

‎07-class-attributes-2.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
classYourClass(object):
2121
classy="class value"
2222

23+
2324
dd=YourClass()
2425
print(dd.classy)# < This should return the string "class value"
2526

2627
dd.classy="Instance value"
27-
print(dd.classy)# This should return the string "Instance value"
28+
print(dd.classy)# This should return the string "Instance value"
2829

2930
# This will delete the value set for 'dd.classy' in the instance.
3031
deldd.classy

‎08-class-instance-attributes-1.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def get_val(self):
2626
defget_count(self):
2727
print(InstanceCounter.count)
2828

29+
2930
a=InstanceCounter(5)
3031
b=InstanceCounter(10)
3132
c=InstanceCounter(15)

‎09-inheritance-1.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ class Time(Date):
2222
defget_time(self):
2323
print("07:00:00")
2424

25+
2526
# Creating an instance from `Date`
2627
dt=Date()
2728
dt.get_date()# Accesing the `get_date()` method of `Date`
2829
print("--------")
2930

3031
# Creating an instance from `Time`.
3132
tm=Time()
32-
tm.get_time()# Accessing the `get_time()` method from `Time`.
33+
tm.get_time()# Accessing the `get_time()` method from `Time`.
3334
# Accessing the `get_date() which is defined in the parent class `Date`.
3435
tm.get_date()

‎10-inheritance-2.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@
1616
# But the instance created from 'Cat' cannot access the attributes
1717
# within the 'Dog' class, and vice versa.
1818

19+
1920
classAnimal(object):
2021
def__init__(self,name):
2122
self.name=name
2223

2324
defeat(self,food):
2425
print("%s is eating %s"% (self.name,food))
2526

27+
2628
classDog(Animal):
2729
deffetch(self,thing):
2830
print("%s goes after the %s"% (self.name,thing))
2931

32+
3033
classCat(Animal):
3134
defswatstring(self):
3235
print("%s shred the string!"%self.name)
3336

37+
3438
d=Dog("Roger")
3539
c=Cat("Fluffy")
3640

@@ -44,4 +48,4 @@ def swatstring(self):
4448
# have access to the other class.
4549

4650
c.fetch("frizbee")
47-
d.swatstring()
51+
d.swatstring()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp