回答編集履歴

10

追記

2019/03/01 16:09

投稿

toshi17922062
toshi17922062

スコア183

test CHANGED
@@ -10,17 +10,25 @@
10
10
 
11
11
  ```python3
12
12
 
13
- # te177123other.py
13
+ #te177123other.py
14
+
15
+
14
16
 
15
17
  x=''
16
18
 
19
+ mainx=''
20
+
17
21
  objclass=''
22
+
23
+
18
24
 
19
25
  dictx={
20
26
 
21
27
  'dx':''
22
28
 
23
29
  }
30
+
31
+
24
32
 
25
33
  def testglobal():
26
34
 
@@ -31,6 +39,8 @@
31
39
  x = ulr
32
40
 
33
41
  dictx['dx']= ulr
42
+
43
+
34
44
 
35
45
  class Testclass():
36
46
 
@@ -66,6 +76,10 @@
66
76
 
67
77
  print("te177123main02:te177123other.dictx['dx']:{}".format(te177123other.dictx['dx']))
68
78
 
79
+ print("te177123main00内でte177123other.mainx指定")
80
+
81
+ print("te177123main02:te177123other.mainx:{}".format(te177123other.mainx))
82
+
69
83
 
70
84
 
71
85
  obj=te177123other.objclass
@@ -97,6 +111,10 @@
97
111
  te177123other.testglobal()
98
112
 
99
113
  te177123other.objclass=te177123other.Testclass()
114
+
115
+ ulr = "ここでulrアクセスする"
116
+
117
+ te177123other.mainx= ulr
100
118
 
101
119
  ```
102
120
 
@@ -130,6 +148,10 @@
130
148
 
131
149
  te177123main02:te177123other.dictx['dx']:ここでulrアクセスする
132
150
 
151
+ te177123main00内でte177123other.mainx指定
152
+
153
+ te177123main02:te177123other.mainx:ここでulrアクセスする
154
+
133
155
  otherのClassインスタンス変数指定
134
156
 
135
157
  te177123main02:te177123other.objclass.ix:ここでulrアクセスする

9

誤字修正

2019/03/01 16:09

投稿

toshi17922062
toshi17922062

スコア183

test CHANGED
@@ -72,15 +72,15 @@
72
72
 
73
73
  print("otherのClassインスタンス変数指定")
74
74
 
75
- print("te177123main02:objclass.ix:{}".format(obj.ix))
75
+ print("te177123main02:te177123other.objclass.ix:{}".format(obj.ix))
76
76
 
77
77
  print("otherのClassインスタンス変数の戻り値指定")
78
78
 
79
- print("te177123main02:objclass.retix():{}".format(obj.retix()))
79
+ print("te177123main02:te177123other.objclass.retix():{}".format(obj.retix()))
80
80
 
81
81
  print("otherのClassクラス変数指定")
82
82
 
83
- print("te177123main02:objclass.cx:{}".format(obj.cx))
83
+ print("te177123main02:te177123other.objclass.cx:{}".format(obj.cx))
84
84
 
85
85
  ```
86
86
 
@@ -120,7 +120,7 @@
120
120
 
121
121
 
122
122
 
123
- ```結果
123
+ ```result
124
124
 
125
125
  otherのglobal指定
126
126
 
@@ -132,15 +132,17 @@
132
132
 
133
133
  otherのClassインスタンス変数指定
134
134
 
135
- te177123main02:obj.ix:ここでulrアクセスする
135
+ te177123main02:te177123other.objclass.ix:ここでulrアクセスする
136
136
 
137
137
  otherのClassインスタンス変数の戻り値指定
138
138
 
139
- te177123main02:obj.retix():ここでulrアクセスする
139
+ te177123main02:te177123other.objclass.retix():ここでulrアクセスする
140
140
 
141
141
  otherのClassクラス変数指定
142
142
 
143
- te177123main02:obj.cx:ここでulrアクセスする
143
+ te177123main02:te177123other.objclass.cx:ここでulrアクセスする
144
+
145
+ test ok
144
146
 
145
147
  ```
146
148
 

8

誤字修正

2019/03/01 15:47

投稿

toshi17922062
toshi17922062

スコア183

test CHANGED
@@ -10,11 +10,11 @@
10
10
 
11
11
  ```python3
12
12
 
13
- #te177123other.py
13
+ # te177123other.py
14
14
 
15
15
  x=''
16
16
 
17
-
17
+ objclass=''
18
18
 
19
19
  dictx={
20
20
 

7

誤字修正

2019/03/01 09:33

投稿

toshi17922062
toshi17922062

スコア183

test CHANGED
@@ -55,8 +55,6 @@
55
55
  #te177123main02.py
56
56
 
57
57
  import te177123other
58
-
59
- import te177123main00
60
58
 
61
59
 
62
60
 

6

バグ修正

2019/03/01 09:27

投稿

toshi17922062
toshi17922062

スコア183

test CHANGED
@@ -70,7 +70,7 @@
70
70
 
71
71
 
72
72
 
73
- obj=te177123main00.objclass
73
+ obj=te177123other.objclass
74
74
 
75
75
  print("otherのClassインスタンス変数指定")
76
76
 
@@ -94,11 +94,11 @@
94
94
 
95
95
  import te177123other
96
96
 
97
- objclass=''
97
+ def init():
98
98
 
99
- te177123other.testglobal()
99
+ te177123other.testglobal()
100
100
 
101
- objclass=te177123other.Testclass()
101
+ te177123other.objclass=te177123other.Testclass()
102
102
 
103
103
  ```
104
104
 
@@ -109,6 +109,8 @@
109
109
  #te177123.py
110
110
 
111
111
  import te177123main00
112
+
113
+ te177123main00.init()
112
114
 
113
115
  import te177123main02
114
116
 

5

追記

2019/03/01 09:24

投稿

toshi17922062
toshi17922062

スコア183

test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  ```python3
12
12
 
13
- # te177123other.py
13
+ #te177123other.py
14
14
 
15
15
  x=''
16
16
 
@@ -22,8 +22,6 @@
22
22
 
23
23
  }
24
24
 
25
-
26
-
27
25
  def testglobal():
28
26
 
29
27
  global x
@@ -33,8 +31,6 @@
33
31
  x = ulr
34
32
 
35
33
  dictx['dx']= ulr
36
-
37
-
38
34
 
39
35
  class Testclass():
40
36
 
@@ -58,13 +54,11 @@
58
54
 
59
55
  #te177123main02.py
60
56
 
57
+ import te177123other
58
+
59
+ import te177123main00
61
60
 
62
61
 
63
- import te177123other
64
-
65
-
66
-
67
- te177123other.testglobal()
68
62
 
69
63
  print("otherのglobal指定")
70
64
 
@@ -76,19 +70,51 @@
76
70
 
77
71
 
78
72
 
79
- obj=te177123other.Testclass()
73
+ obj=te177123main00.objclass
80
74
 
81
75
  print("otherのClassインスタンス変数指定")
82
76
 
83
- print("te177123main02:obj.ix:{}".format(obj.ix))
77
+ print("te177123main02:objclass.ix:{}".format(obj.ix))
84
78
 
85
79
  print("otherのClassインスタンス変数の戻り値指定")
86
80
 
87
- print("te177123main02:obj.retix():{}".format(obj.retix()))
81
+ print("te177123main02:objclass.retix():{}".format(obj.retix()))
88
82
 
89
83
  print("otherのClassクラス変数指定")
90
84
 
91
- print("te177123main02:obj.cx:{}".format(obj.cx))
85
+ print("te177123main02:objclass.cx:{}".format(obj.cx))
86
+
87
+ ```
88
+
89
+
90
+
91
+ ```python3
92
+
93
+ #te177123main00.py
94
+
95
+ import te177123other
96
+
97
+ objclass=''
98
+
99
+ te177123other.testglobal()
100
+
101
+ objclass=te177123other.Testclass()
102
+
103
+ ```
104
+
105
+
106
+
107
+ ```python3
108
+
109
+ #te177123.py
110
+
111
+ import te177123main00
112
+
113
+ import te177123main02
114
+
115
+
116
+
117
+ print("test ok")
92
118
 
93
119
  ```
94
120
 
@@ -118,8 +144,12 @@
118
144
 
119
145
  ```
120
146
 
147
+ こんな感じで間違ってないですかね?ちょっと自信ないですが。
121
148
 
149
+ te177123.py(主プログラム)
122
150
 
123
- で、思ったんですがclassをそれぞれのファイルでインスタンスを生成するのはだめなんですね。
151
+ te177123other.py(定義)
124
152
 
125
- どのタイミングかで別途インスタンス生成し、それをそれぞれのファイルで共有するとなると、最終的にはそのインスタンスをglobal変数とか辞書とかのパッケージに入れるしかないのでしょうかね。
153
+ te177123main00.py(URL設定するプログラム)
154
+
155
+ te177123main02.py(それぞれのプログラム)

4

追記

2019/03/01 09:10

投稿

toshi17922062
toshi17922062

スコア183

test CHANGED
@@ -117,3 +117,9 @@
117
117
  te177123main02:obj.cx:ここでulrアクセスする
118
118
 
119
119
  ```
120
+
121
+
122
+
123
+ で、思ったんですがclassをそれぞれのファイルでインスタンスを生成するのはだめなんですね。
124
+
125
+ どのタイミングかで別途インスタンスを生成し、それをそれぞれのファイルで共有するとなると、最終的にはそのインスタンスをglobal変数とか辞書とかのパッケージに入れるしかないのでしょうかね。

3

誤字修正

2019/03/01 08:45

投稿

toshi17922062
toshi17922062

スコア183

test CHANGED
@@ -76,19 +76,19 @@
76
76
 
77
77
 
78
78
 
79
- ojt=te177123other.Testclass()
79
+ obj=te177123other.Testclass()
80
80
 
81
81
  print("otherのClassインスタンス変数指定")
82
82
 
83
- print("te177123main02:ojt.ix:{}".format(ojt.ix))
83
+ print("te177123main02:obj.ix:{}".format(obj.ix))
84
84
 
85
85
  print("otherのClassインスタンス変数の戻り値指定")
86
86
 
87
- print("te177123main02:ojt.retix():{}".format(ojt.retix()))
87
+ print("te177123main02:obj.retix():{}".format(obj.retix()))
88
88
 
89
89
  print("otherのClassクラス変数指定")
90
90
 
91
- print("te177123main02:ojt.cx:{}".format(ojt.cx))
91
+ print("te177123main02:obj.cx:{}".format(obj.cx))
92
92
 
93
93
  ```
94
94
 
@@ -106,14 +106,14 @@
106
106
 
107
107
  otherのClassインスタンス変数指定
108
108
 
109
- te177123main02:ojt.ix:ここでulrアクセスする
109
+ te177123main02:obj.ix:ここでulrアクセスする
110
110
 
111
111
  otherのClassインスタンス変数の戻り値指定
112
112
 
113
- te177123main02:ojt.retix():ここでulrアクセスする
113
+ te177123main02:obj.retix():ここでulrアクセスする
114
114
 
115
115
  otherのClassクラス変数指定
116
116
 
117
- te177123main02:ojt.cx:ここでulrアクセスする
117
+ te177123main02:obj.cx:ここでulrアクセスする
118
118
 
119
119
  ```

2

誤字修正

2019/03/01 08:24

投稿

toshi17922062
toshi17922062

スコア183

test CHANGED
@@ -37,8 +37,6 @@
37
37
 
38
38
 
39
39
  class Testclass():
40
-
41
- x=''
42
40
 
43
41
  def __init__(self):
44
42
 

1

追記

2019/03/01 08:19

投稿

toshi17922062
toshi17922062

スコア183

test CHANGED
@@ -1,3 +1,121 @@
1
1
  既出のこの内容そのものと思われますが、いかがでしょう?個人的にはクラス化するか、もし複数の共通変数が存在するなら、まとめて共通で使用する変数を辞書にしてしまう手とか?
2
2
 
3
3
  [Python - Python 外部ファイルで定義した変数を使用したい|teratail](https://teratail.com/questions/51381)
4
+
5
+
6
+
7
+ とりあえず、以上をまとめてみました。まだ初心者なので違っていたらすいません。
8
+
9
+
10
+
11
+ ```python3
12
+
13
+ # te177123other.py
14
+
15
+ x=''
16
+
17
+
18
+
19
+ dictx={
20
+
21
+ 'dx':''
22
+
23
+ }
24
+
25
+
26
+
27
+ def testglobal():
28
+
29
+ global x
30
+
31
+ ulr = "ここでulrアクセスする"
32
+
33
+ x = ulr
34
+
35
+ dictx['dx']= ulr
36
+
37
+
38
+
39
+ class Testclass():
40
+
41
+ x=''
42
+
43
+ def __init__(self):
44
+
45
+ ulr = "ここでulrアクセスする"
46
+
47
+ Testclass.cx = ulr
48
+
49
+ self.ix= ulr
50
+
51
+ def retix(self):
52
+
53
+ return self.ix
54
+
55
+ ```
56
+
57
+
58
+
59
+ ```python3
60
+
61
+ #te177123main02.py
62
+
63
+
64
+
65
+ import te177123other
66
+
67
+
68
+
69
+ te177123other.testglobal()
70
+
71
+ print("otherのglobal指定")
72
+
73
+ print("te177123main02:te177123other.x:{}".format(te177123other.x))
74
+
75
+ print("otherの辞書変数指定")
76
+
77
+ print("te177123main02:te177123other.dictx['dx']:{}".format(te177123other.dictx['dx']))
78
+
79
+
80
+
81
+ ojt=te177123other.Testclass()
82
+
83
+ print("otherのClassインスタンス変数指定")
84
+
85
+ print("te177123main02:ojt.ix:{}".format(ojt.ix))
86
+
87
+ print("otherのClassインスタンス変数の戻り値指定")
88
+
89
+ print("te177123main02:ojt.retix():{}".format(ojt.retix()))
90
+
91
+ print("otherのClassクラス変数指定")
92
+
93
+ print("te177123main02:ojt.cx:{}".format(ojt.cx))
94
+
95
+ ```
96
+
97
+
98
+
99
+ ```結果
100
+
101
+ otherのglobal指定
102
+
103
+ te177123main02:te177123other.x:ここでulrアクセスする
104
+
105
+ otherの辞書変数指定
106
+
107
+ te177123main02:te177123other.dictx['dx']:ここでulrアクセスする
108
+
109
+ otherのClassインスタンス変数指定
110
+
111
+ te177123main02:ojt.ix:ここでulrアクセスする
112
+
113
+ otherのClassインスタンス変数の戻り値指定
114
+
115
+ te177123main02:ojt.retix():ここでulrアクセスする
116
+
117
+ otherのClassクラス変数指定
118
+
119
+ te177123main02:ojt.cx:ここでulrアクセスする
120
+
121
+ ```