質問編集履歴
6
書式の改善
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
いま、例えばあるクラスFooを作ろうとしているときに、2パターンのコードを考えています。
|
4
4
|
|
5
5
|
①
|
6
|
+
```Python
|
6
7
|
class Foo(object):
|
7
8
|
def __init__(self, n):
|
8
9
|
self.x = n
|
@@ -14,7 +15,10 @@
|
|
14
15
|
y = do_something(x)
|
15
16
|
return y
|
16
17
|
|
18
|
+
```
|
19
|
+
|
17
20
|
②
|
21
|
+
```Python
|
18
22
|
class Foo(object):
|
19
23
|
def __init__(self, n):
|
20
24
|
self.x = n
|
@@ -23,7 +27,9 @@
|
|
23
27
|
|
24
28
|
def set_y(self):
|
25
29
|
self.y = do_something(self.x)
|
30
|
+
```
|
26
31
|
|
32
|
+
|
27
33
|
①は、calc_yで使用する変数を明示しているので、クラス変数xの変更がこれに影響すること、変数zの変更はこれに全く影響しないことが明示されます。
|
28
34
|
|
29
35
|
しかし調べた限りでは@staticmethodは可能なら使わないほうが良いという記事が多く、その点では②にすべきであるように思えます。
|
5
title
CHANGED
File without changes
|
body
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
def set_y(self):
|
25
25
|
self.y = do_something(self.x)
|
26
26
|
|
27
|
-
①は、calc_yで使用する明示しているので、クラス変数
|
27
|
+
①は、calc_yで使用する変数を明示しているので、クラス変数xの変更がこれに影響すること、変数zの変更はこれに全く影響しないことが明示されます。
|
28
28
|
|
29
29
|
しかし調べた限りでは@staticmethodは可能なら使わないほうが良いという記事が多く、その点では②にすべきであるように思えます。
|
30
30
|
|
4
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
①
|
6
6
|
class Foo(object):
|
7
|
-
def
|
7
|
+
def __init__(self, n):
|
8
8
|
self.x = n
|
9
9
|
self.z = 3
|
10
10
|
self.y = self.calc_y(x)
|
@@ -16,7 +16,7 @@
|
|
16
16
|
|
17
17
|
②
|
18
18
|
class Foo(object):
|
19
|
-
def
|
19
|
+
def __init__(self, n):
|
20
20
|
self.x = n
|
21
21
|
self.z = 3
|
22
22
|
self.set_y()
|
3
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,27 +4,27 @@
|
|
4
4
|
|
5
5
|
①
|
6
6
|
class Foo(object):
|
7
|
-
def ¥
|
7
|
+
def ¥__init¥__(self, n):
|
8
8
|
self.x = n
|
9
9
|
self.z = 3
|
10
|
-
self.y = self.
|
10
|
+
self.y = self.calc_y(x)
|
11
11
|
|
12
12
|
@staticmethod
|
13
|
-
def
|
13
|
+
def calc_y(x):
|
14
|
-
y =
|
14
|
+
y = do_something(x)
|
15
15
|
return y
|
16
16
|
|
17
17
|
②
|
18
18
|
class Foo(object):
|
19
|
-
def ¥
|
19
|
+
def \__init¥__(self, n):
|
20
20
|
self.x = n
|
21
21
|
self.z = 3
|
22
|
-
self.
|
22
|
+
self.set_y()
|
23
23
|
|
24
|
-
def
|
24
|
+
def set_y(self):
|
25
|
-
self.y =
|
25
|
+
self.y = do_something(self.x)
|
26
26
|
|
27
|
-
①は、
|
27
|
+
①は、calc_yで使用する明示しているので、クラス変数変数xの変更がこれに影響すること、変数zの変更はこれに全く影響しないことが明示されます。
|
28
28
|
|
29
29
|
しかし調べた限りでは@staticmethodは可能なら使わないほうが良いという記事が多く、その点では②にすべきであるように思えます。
|
30
30
|
|
2
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,27 +4,27 @@
|
|
4
4
|
|
5
5
|
①
|
6
6
|
class Foo(object):
|
7
|
-
def
|
7
|
+
def ¥_¥_init¥_¥_(self, n):
|
8
8
|
self.x = n
|
9
9
|
self.z = 3
|
10
|
-
self.y = self.calc
|
10
|
+
self.y = self.calc¥_y(x)
|
11
11
|
|
12
12
|
@staticmethod
|
13
|
-
def calc
|
13
|
+
def calc¥_y(x):
|
14
|
-
y = do
|
14
|
+
y = do¥_something(x)
|
15
15
|
return y
|
16
16
|
|
17
17
|
②
|
18
18
|
class Foo(object):
|
19
|
-
def
|
19
|
+
def ¥_¥_init¥_¥_(self, n):
|
20
20
|
self.x = n
|
21
21
|
self.z = 3
|
22
|
-
self.set
|
22
|
+
self.set¥_y()
|
23
23
|
|
24
|
-
def set
|
24
|
+
def set¥_y(self):
|
25
|
-
self.y = do
|
25
|
+
self.y = do¥_something(self.x)
|
26
26
|
|
27
|
-
①は、calc
|
27
|
+
①は、calc¥_yで使用する明示しているので、クラス変数変数xの変更がこれに影響すること、変数zの変更はこれに全く影響しないことが明示されます。
|
28
28
|
|
29
29
|
しかし調べた限りでは@staticmethodは可能なら使わないほうが良いという記事が多く、その点では②にすべきであるように思えます。
|
30
30
|
|
1
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,27 +4,27 @@
|
|
4
4
|
|
5
5
|
①
|
6
6
|
class Foo(object):
|
7
|
-
def
|
7
|
+
def \_\_init\_\_(self, n):
|
8
8
|
self.x = n
|
9
9
|
self.z = 3
|
10
|
-
self.y = self.
|
10
|
+
self.y = self.calc\_y(x)
|
11
11
|
|
12
12
|
@staticmethod
|
13
|
-
def
|
13
|
+
def calc\_y(x):
|
14
|
-
y =
|
14
|
+
y = do\_something(x)
|
15
15
|
return y
|
16
16
|
|
17
17
|
②
|
18
18
|
class Foo(object):
|
19
|
-
def
|
19
|
+
def \_\_init\_\_(self, n):
|
20
20
|
self.x = n
|
21
21
|
self.z = 3
|
22
|
-
self.
|
22
|
+
self.set\_y()
|
23
23
|
|
24
|
-
def
|
24
|
+
def set\_y(self):
|
25
|
-
self.y =
|
25
|
+
self.y = do\_something(self.x)
|
26
26
|
|
27
|
-
①は、
|
27
|
+
①は、calc\_yで使用する明示しているので、クラス変数変数xの変更がこれに影響すること、変数zの変更はこれに全く影響しないことが明示されます。
|
28
28
|
|
29
29
|
しかし調べた限りでは@staticmethodは可能なら使わないほうが良いという記事が多く、その点では②にすべきであるように思えます。
|
30
30
|
|