Skip to content

Commit bce89b7

Browse files
author
tan00
committed
第8章结束 类结束
1 parent 6f9bae9 commit bce89b7

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

src/8/calling_a_method_on_an_object_given_the_name_as_a_string/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def distance(self, x, y):
2020

2121
# Method 2: Use methodcaller
2222
import operator
23-
d = operator.methodcaller('distance', 0, 0)(p)
23+
d = operator.methodcaller('diistance', 0, 0)(p)
2424
print(d)
2525

2626
# Application in sorting

src/8/creating_an_instance_without_invoking_init/example.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@ def today(cls):
1616
d.day = t.tm_mday
1717
return d
1818

19-
d = Date.__new__(Date)
20-
print(d)
21-
print(hasattr(d,'year'))
19+
# d = Date.__new__(Date)
20+
# print(d)
21+
# print(hasattr(d,'year'))
2222

23-
data = {
24-
'year' : 2012,
25-
'month' : 8,
26-
'day' : 29
27-
}
23+
# data = {
24+
# 'year' : 2012,
25+
# 'month' : 8,
26+
# 'day' : 29
27+
# }
2828

29-
d.__dict__.update(data)
30-
print(d.year)
31-
print(d.month)
29+
# d.__dict__.update(data)
30+
# print(d.year)
31+
# print(d.month)
3232

3333
d = Date.today()
34+
3435
print(d.year, d.month, d.day)
3536

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from time import localtime
2+
3+
class Date:
4+
def __init__(self, year, month, day):
5+
self.year = year
6+
self.month = month
7+
self.day = day
8+
9+
10+
@classmethod
11+
def today(cls):
12+
t = localtime()
13+
return cls(t.tm_year,t.tm_mon,t.tm_mday)
14+
15+
16+
d = Date.today()
17+
18+

0 commit comments

Comments
 (0)