@staticmethod und @classmethod
Die slides befinden sich im Anhang dieser Seite. Zur "schöneren" Darstellung muss der String $REVEALJS_ROOT im file auf eine valide reveal.js location geändert werden - reveal.js ist jedenfalls mal einen Blick wert!
1 class A(object):
2 def foo(self, x):
3 print("executing foo({}, {})".format(self, x))
4
5 @classmethod
6 def class_foo(cls, x):
7 print("executing class_foo({}, {})".format(cls, x))
8
9 @staticmethod
10 def static_foo(x):
11 print("executing static_foo({})".format(x))
12
13 a = A()
14
15 a.foo(1)
16
17 a.foo
18
19 A.class_foo(1)
20
21 A.class_foo
22
23 a.class_foo(1)
24
25 a.static_foo(1)
26
27 A.static_foo('hi')
28
29
30 class Date(object):
31 def __init__(self, year=0, month=0, day=0):
32 self.day = day
33 self.month = month
34 self.year = year
35
36 def __repr__(self):
37 return "It's the {}.{}.{}".format(self.day, self.month, self.year)
38
39 Date(2014, 4, 1)
40
41 string_date = '01-04-2014'
42 day, month, year = map(int, string_date.split('-'))
43 date1 = Date(year, month, day)
44 date1
45
46
47 class Date(object):
48 def __init__(self, year=0, month=0, day=0):
49 self.day = day
50 self.month = month
51 self.year = year
52
53 def __repr__(self):
54 return "It's the {}.{}.{}".format(self.day, self.month, self.year)
55
56 @classmethod
57 def from_string(cls, date_as_string):
58 day, month, year = map(int, date_as_string.split('-'))
59 date1 = cls(year, month, day)
60 return date1
61
62 Date.from_string('01-04-2014')
63
64 date_as_string = '01-04-2014'
65 day, month, year = map(int, date_as_string.split('-'))
66 day <= 31 and month <= 12 and year <= 3999
67
68
69 class Date(object):
70 def __init__(self, year=0, month=0, day=0):
71 self.day = day
72 self.month = month
73 self.year = year
74
75 def __repr__(self):
76 return "It's the {}.{}.{}".format(self.day, self.month, self.year)
77
78 @staticmethod
79 def is_date_valid(date_as_string):
80 day, month, year = map(int, date_as_string.split('-'))
81 return day <= 31 and month <= 12 and year <= 3999
82
83 Date.is_date_valid('01-04-2014')