프로그램
[파이썬] 문제 : 클래스에서 외부 def 함수 사용
오디세이99
2022. 12. 3. 20:39
728x90
반응형
(문제)
파이썬 class 외부 def 를 통한 변수 출력
import random
import time
def external_def():
global out_def
# while 1:
external_var = 'def 내 while 문 : '
out0 = random.randint(10, 50)
out_def = external_var + str(out0)
time.sleep(0.1)
print(out_def)
class other:
def get_var( self ):
class_text = 'class 내 run 확인 : '
out_class = class_text + out_def
print(out_class)
# return out_class
if __name__=="__main__":
run_other = other()
run_other.get_var()
위 코드를 돌리면 아래와 같이 나옵니다.
name 'out_def' is not defined
class 문에서는 out_def 변수를 실시간 가지고 와서 연산하고 싶습니다.
제가 원하는 것은 결과는 아래와 같습니다.
예)
class 내 run 확인 : def 내 while 문 : 44
class 내 run 확인 : def 내 while 문 : 43
class 내 run 확인 : def 내 while 문 : 44
class 내 run 확인 : def 내 while 문 : 41
class 내 run 확인 : def 내 while 문 : 41
(방법)
import random
import time
def external_def():
global out_def
# while 1:
external_var = 'def 내 while 문 : '
out0 = random.randint(10, 50)
out_def = external_var + str(out0)
time.sleep(0.1)
# print(out_def)
# return out_def
class other:
def get_var( self ):
cnt = 0
while cnt < 10:
cnt += 1
class_text = 'class 내 run 확인 : '
external_def()
out_class = class_text + out_def
print(out_class)
# return out_class
if __name__=="__main__":
run_other = other()
run_other.get_var()

728x90
반응형