-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass vs static
More file actions
32 lines (24 loc) · 1.07 KB
/
class vs static
File metadata and controls
32 lines (24 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Good explanation of classmethod vs staticmethod.
http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner
http://stackoverflow.com/questions/735975/static-methods-in-python
class Date(object):
day = 0
month = 0
year = 0
def __init__(self, day=0, month=0, year=0):
self.day = day
self.month = month
self.year = year
@classmethod
def from_string(cls, date_as_string):
day, month, year = map(int, date_as_string.split('-'))
date1 = cls(day, month, year)
return date1
@staticmethod
def is_date_valid(date_as_string):
day, month, year = map(int, date_as_string.split('-'))
return day <= 31 and month <= 12 and year <= 3999
So, as we can see from usage of staticmethod, we don't have any access to what the class is- it's basically just a
function, called syntactically like a method, but without access to the object and it's internals (fields and another
methods), while classmethod does.
date2 = Date.from_string('11-09-2012')