teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

10

追記

2019/03/01 16:09

投稿

toshi17922062
toshi17922062

スコア183

answer CHANGED
@@ -4,17 +4,22 @@
4
4
  とりあえず、以上をまとめてみました。まだ初心者なので違っていたらすいません。
5
5
 
6
6
  ```python3
7
- # te177123other.py
7
+ #te177123other.py
8
+
8
9
  x=''
10
+ mainx=''
9
11
  objclass=''
12
+
10
13
  dictx={
11
14
  'dx':''
12
15
  }
16
+
13
17
  def testglobal():
14
18
  global x
15
19
  ulr = "ここでulrアクセスする"
16
20
  x = ulr
17
21
  dictx['dx']= ulr
22
+
18
23
  class Testclass():
19
24
  def __init__(self):
20
25
  ulr = "ここでulrアクセスする"
@@ -32,6 +37,8 @@
32
37
  print("te177123main02:te177123other.x:{}".format(te177123other.x))
33
38
  print("otherの辞書変数指定")
34
39
  print("te177123main02:te177123other.dictx['dx']:{}".format(te177123other.dictx['dx']))
40
+ print("te177123main00内でte177123other.mainx指定")
41
+ print("te177123main02:te177123other.mainx:{}".format(te177123other.mainx))
35
42
 
36
43
  obj=te177123other.objclass
37
44
  print("otherのClassインスタンス変数指定")
@@ -48,6 +55,8 @@
48
55
  def init():
49
56
  te177123other.testglobal()
50
57
  te177123other.objclass=te177123other.Testclass()
58
+ ulr = "ここでulrアクセスする"
59
+ te177123other.mainx= ulr
51
60
  ```
52
61
 
53
62
  ```python3
@@ -64,6 +73,8 @@
64
73
  te177123main02:te177123other.x:ここでulrアクセスする
65
74
  otherの辞書変数指定
66
75
  te177123main02:te177123other.dictx['dx']:ここでulrアクセスする
76
+ te177123main00内でte177123other.mainx指定
77
+ te177123main02:te177123other.mainx:ここでulrアクセスする
67
78
  otherのClassインスタンス変数指定
68
79
  te177123main02:te177123other.objclass.ix:ここでulrアクセスする
69
80
  otherのClassインスタンス変数の戻り値指定

9

誤字修正

2019/03/01 16:09

投稿

toshi17922062
toshi17922062

スコア183

answer CHANGED
@@ -35,11 +35,11 @@
35
35
 
36
36
  obj=te177123other.objclass
37
37
  print("otherのClassインスタンス変数指定")
38
- print("te177123main02:objclass.ix:{}".format(obj.ix))
38
+ print("te177123main02:te177123other.objclass.ix:{}".format(obj.ix))
39
39
  print("otherのClassインスタンス変数の戻り値指定")
40
- print("te177123main02:objclass.retix():{}".format(obj.retix()))
40
+ print("te177123main02:te177123other.objclass.retix():{}".format(obj.retix()))
41
41
  print("otherのClassクラス変数指定")
42
- print("te177123main02:objclass.cx:{}".format(obj.cx))
42
+ print("te177123main02:te177123other.objclass.cx:{}".format(obj.cx))
43
43
  ```
44
44
 
45
45
  ```python3
@@ -59,17 +59,18 @@
59
59
  print("test ok")
60
60
  ```
61
61
 
62
- ```結果
62
+ ```result
63
63
  otherのglobal指定
64
64
  te177123main02:te177123other.x:ここでulrアクセスする
65
65
  otherの辞書変数指定
66
66
  te177123main02:te177123other.dictx['dx']:ここでulrアクセスする
67
67
  otherのClassインスタンス変数指定
68
- te177123main02:obj.ix:ここでulrアクセスする
68
+ te177123main02:te177123other.objclass.ix:ここでulrアクセスする
69
69
  otherのClassインスタンス変数の戻り値指定
70
- te177123main02:obj.retix():ここでulrアクセスする
70
+ te177123main02:te177123other.objclass.retix():ここでulrアクセスする
71
71
  otherのClassクラス変数指定
72
- te177123main02:obj.cx:ここでulrアクセスする
72
+ te177123main02:te177123other.objclass.cx:ここでulrアクセスする
73
+ test ok
73
74
  ```
74
75
  こんな感じで間違ってないですかね?ちょっと自信ないですが。
75
76
  te177123.py(主プログラム)

8

誤字修正

2019/03/01 15:47

投稿

toshi17922062
toshi17922062

スコア183

answer CHANGED
@@ -4,9 +4,9 @@
4
4
  とりあえず、以上をまとめてみました。まだ初心者なので違っていたらすいません。
5
5
 
6
6
  ```python3
7
- #te177123other.py
7
+ # te177123other.py
8
8
  x=''
9
-
9
+ objclass=''
10
10
  dictx={
11
11
  'dx':''
12
12
  }

7

誤字修正

2019/03/01 09:33

投稿

toshi17922062
toshi17922062

スコア183

answer CHANGED
@@ -27,7 +27,6 @@
27
27
  ```python3
28
28
  #te177123main02.py
29
29
  import te177123other
30
- import te177123main00
31
30
 
32
31
  print("otherのglobal指定")
33
32
  print("te177123main02:te177123other.x:{}".format(te177123other.x))

6

バグ修正

2019/03/01 09:27

投稿

toshi17922062
toshi17922062

スコア183

answer CHANGED
@@ -34,7 +34,7 @@
34
34
  print("otherの辞書変数指定")
35
35
  print("te177123main02:te177123other.dictx['dx']:{}".format(te177123other.dictx['dx']))
36
36
 
37
- obj=te177123main00.objclass
37
+ obj=te177123other.objclass
38
38
  print("otherのClassインスタンス変数指定")
39
39
  print("te177123main02:objclass.ix:{}".format(obj.ix))
40
40
  print("otherのClassインスタンス変数の戻り値指定")
@@ -46,14 +46,15 @@
46
46
  ```python3
47
47
  #te177123main00.py
48
48
  import te177123other
49
- objclass=''
49
+ def init():
50
- te177123other.testglobal()
50
+ te177123other.testglobal()
51
- objclass=te177123other.Testclass()
51
+ te177123other.objclass=te177123other.Testclass()
52
52
  ```
53
53
 
54
54
  ```python3
55
55
  #te177123.py
56
56
  import te177123main00
57
+ te177123main00.init()
57
58
  import te177123main02
58
59
 
59
60
  print("test ok")

5

追記

2019/03/01 09:24

投稿

toshi17922062
toshi17922062

スコア183

answer CHANGED
@@ -4,19 +4,17 @@
4
4
  とりあえず、以上をまとめてみました。まだ初心者なので違っていたらすいません。
5
5
 
6
6
  ```python3
7
- # te177123other.py
7
+ #te177123other.py
8
8
  x=''
9
9
 
10
10
  dictx={
11
11
  'dx':''
12
12
  }
13
-
14
13
  def testglobal():
15
14
  global x
16
15
  ulr = "ここでulrアクセスする"
17
16
  x = ulr
18
17
  dictx['dx']= ulr
19
-
20
18
  class Testclass():
21
19
  def __init__(self):
22
20
  ulr = "ここでulrアクセスする"
@@ -28,24 +26,39 @@
28
26
 
29
27
  ```python3
30
28
  #te177123main02.py
31
-
32
29
  import te177123other
30
+ import te177123main00
33
31
 
34
- te177123other.testglobal()
35
32
  print("otherのglobal指定")
36
33
  print("te177123main02:te177123other.x:{}".format(te177123other.x))
37
34
  print("otherの辞書変数指定")
38
35
  print("te177123main02:te177123other.dictx['dx']:{}".format(te177123other.dictx['dx']))
39
36
 
40
- obj=te177123other.Testclass()
37
+ obj=te177123main00.objclass
41
38
  print("otherのClassインスタンス変数指定")
42
- print("te177123main02:obj.ix:{}".format(obj.ix))
39
+ print("te177123main02:objclass.ix:{}".format(obj.ix))
43
40
  print("otherのClassインスタンス変数の戻り値指定")
44
- print("te177123main02:obj.retix():{}".format(obj.retix()))
41
+ print("te177123main02:objclass.retix():{}".format(obj.retix()))
45
42
  print("otherのClassクラス変数指定")
46
- print("te177123main02:obj.cx:{}".format(obj.cx))
43
+ print("te177123main02:objclass.cx:{}".format(obj.cx))
47
44
  ```
48
45
 
46
+ ```python3
47
+ #te177123main00.py
48
+ import te177123other
49
+ objclass=''
50
+ te177123other.testglobal()
51
+ objclass=te177123other.Testclass()
52
+ ```
53
+
54
+ ```python3
55
+ #te177123.py
56
+ import te177123main00
57
+ import te177123main02
58
+
59
+ print("test ok")
60
+ ```
61
+
49
62
  ```結果
50
63
  otherのglobal指定
51
64
  te177123main02:te177123other.x:ここでulrアクセスする
@@ -58,6 +71,8 @@
58
71
  otherのClassクラス変数指定
59
72
  te177123main02:obj.cx:ここでulrアクセスする
60
73
  ```
61
-
62
- 、思たんですがclassをそれぞれのファイルでインスタンスを生成するのはだめです
74
+ こんな感じ間違てないですかね?ちょっと自信です
75
+ te177123.py(主プログラム)
76
+ te177123other.py(定義)
63
- どのタイミングかで別途インスタンス生成し、それをそれぞれのファイルで共有するとなると、最終的にはそのインスタンスをglobal変数とか辞書とかのパッケージに入れるしかないのでしょうかね。
77
+ te177123main00.py(URL設定するプログラム)
78
+ te177123main02.py(それぞれのプログラム)

4

追記

2019/03/01 09:10

投稿

toshi17922062
toshi17922062

スコア183

answer CHANGED
@@ -57,4 +57,7 @@
57
57
  te177123main02:obj.retix():ここでulrアクセスする
58
58
  otherのClassクラス変数指定
59
59
  te177123main02:obj.cx:ここでulrアクセスする
60
- ```
60
+ ```
61
+
62
+ で、思ったんですがclassをそれぞれのファイルでインスタンスを生成するのはだめなんですね。
63
+ どのタイミングかで別途インスタンスを生成し、それをそれぞれのファイルで共有するとなると、最終的にはそのインスタンスをglobal変数とか辞書とかのパッケージに入れるしかないのでしょうかね。

3

誤字修正

2019/03/01 08:45

投稿

toshi17922062
toshi17922062

スコア183

answer CHANGED
@@ -37,13 +37,13 @@
37
37
  print("otherの辞書変数指定")
38
38
  print("te177123main02:te177123other.dictx['dx']:{}".format(te177123other.dictx['dx']))
39
39
 
40
- ojt=te177123other.Testclass()
40
+ obj=te177123other.Testclass()
41
41
  print("otherのClassインスタンス変数指定")
42
- print("te177123main02:ojt.ix:{}".format(ojt.ix))
42
+ print("te177123main02:obj.ix:{}".format(obj.ix))
43
43
  print("otherのClassインスタンス変数の戻り値指定")
44
- print("te177123main02:ojt.retix():{}".format(ojt.retix()))
44
+ print("te177123main02:obj.retix():{}".format(obj.retix()))
45
45
  print("otherのClassクラス変数指定")
46
- print("te177123main02:ojt.cx:{}".format(ojt.cx))
46
+ print("te177123main02:obj.cx:{}".format(obj.cx))
47
47
  ```
48
48
 
49
49
  ```結果
@@ -52,9 +52,9 @@
52
52
  otherの辞書変数指定
53
53
  te177123main02:te177123other.dictx['dx']:ここでulrアクセスする
54
54
  otherのClassインスタンス変数指定
55
- te177123main02:ojt.ix:ここでulrアクセスする
55
+ te177123main02:obj.ix:ここでulrアクセスする
56
56
  otherのClassインスタンス変数の戻り値指定
57
- te177123main02:ojt.retix():ここでulrアクセスする
57
+ te177123main02:obj.retix():ここでulrアクセスする
58
58
  otherのClassクラス変数指定
59
- te177123main02:ojt.cx:ここでulrアクセスする
59
+ te177123main02:obj.cx:ここでulrアクセスする
60
60
  ```

2

誤字修正

2019/03/01 08:24

投稿

toshi17922062
toshi17922062

スコア183

answer CHANGED
@@ -18,7 +18,6 @@
18
18
  dictx['dx']= ulr
19
19
 
20
20
  class Testclass():
21
- x=''
22
21
  def __init__(self):
23
22
  ulr = "ここでulrアクセスする"
24
23
  Testclass.cx = ulr

1

追記

2019/03/01 08:19

投稿

toshi17922062
toshi17922062

スコア183

answer CHANGED
@@ -1,2 +1,61 @@
1
1
  既出のこの内容そのものと思われますが、いかがでしょう?個人的にはクラス化するか、もし複数の共通変数が存在するなら、まとめて共通で使用する変数を辞書にしてしまう手とか?
2
- [Python - Python 外部ファイルで定義した変数を使用したい|teratail](https://teratail.com/questions/51381)
2
+ [Python - Python 外部ファイルで定義した変数を使用したい|teratail](https://teratail.com/questions/51381)
3
+
4
+ とりあえず、以上をまとめてみました。まだ初心者なので違っていたらすいません。
5
+
6
+ ```python3
7
+ # te177123other.py
8
+ x=''
9
+
10
+ dictx={
11
+ 'dx':''
12
+ }
13
+
14
+ def testglobal():
15
+ global x
16
+ ulr = "ここでulrアクセスする"
17
+ x = ulr
18
+ dictx['dx']= ulr
19
+
20
+ class Testclass():
21
+ x=''
22
+ def __init__(self):
23
+ ulr = "ここでulrアクセスする"
24
+ Testclass.cx = ulr
25
+ self.ix= ulr
26
+ def retix(self):
27
+ return self.ix
28
+ ```
29
+
30
+ ```python3
31
+ #te177123main02.py
32
+
33
+ import te177123other
34
+
35
+ te177123other.testglobal()
36
+ print("otherのglobal指定")
37
+ print("te177123main02:te177123other.x:{}".format(te177123other.x))
38
+ print("otherの辞書変数指定")
39
+ print("te177123main02:te177123other.dictx['dx']:{}".format(te177123other.dictx['dx']))
40
+
41
+ ojt=te177123other.Testclass()
42
+ print("otherのClassインスタンス変数指定")
43
+ print("te177123main02:ojt.ix:{}".format(ojt.ix))
44
+ print("otherのClassインスタンス変数の戻り値指定")
45
+ print("te177123main02:ojt.retix():{}".format(ojt.retix()))
46
+ print("otherのClassクラス変数指定")
47
+ print("te177123main02:ojt.cx:{}".format(ojt.cx))
48
+ ```
49
+
50
+ ```結果
51
+ otherのglobal指定
52
+ te177123main02:te177123other.x:ここでulrアクセスする
53
+ otherの辞書変数指定
54
+ te177123main02:te177123other.dictx['dx']:ここでulrアクセスする
55
+ otherのClassインスタンス変数指定
56
+ te177123main02:ojt.ix:ここでulrアクセスする
57
+ otherのClassインスタンス変数の戻り値指定
58
+ te177123main02:ojt.retix():ここでulrアクセスする
59
+ otherのClassクラス変数指定
60
+ te177123main02:ojt.cx:ここでulrアクセスする
61
+ ```