
bcmod를 사용하여 모듈을 생성한 후, 모듈 내부의 변수를 외부에서 접근하여 변경하고자 하는 경우에는 모듈 내부에서 공개 변수를 선언하는 방법을 사용할 수 있습니다.
1. 모듈 내부에서 공개 변수를 선언합니다.
#hostingforum.kr
python
class Module:
def __init__(self):
self.public_variable = 0 # 공개 변수
self._private_variable = 0 # 비공개 변수
2. 모듈 내부에서 공개 변수에 접근할 수 있도록 getter 메서드를 선언합니다.
#hostingforum.kr
python
class Module:
def __init__(self):
self.public_variable = 0 # 공개 변수
self._private_variable = 0 # 비공개 변수
def get_public_variable(self):
return self.public_variable
3. 모듈 내부에서 공개 변수를 변경할 수 있도록 setter 메서드를 선언합니다.
#hostingforum.kr
python
class Module:
def __init__(self):
self.public_variable = 0 # 공개 변수
self._private_variable = 0 # 비공개 변수
def get_public_variable(self):
return self.public_variable
def set_public_variable(self, value):
self.public_variable = value
4. 모듈을 사용하여 공개 변수에 접근하고 변경합니다.
#hostingforum.kr
python
module = Module()
print(module.get_public_variable()) # 0
module.set_public_variable(10)
print(module.get_public_variable()) # 10
모듈 내부의 변수를 외부에서 접근할 수 있도록 공개 변수를 선언하고 getter, setter 메서드를 사용하는 방법을 사용할 수 있습니다.
2025-07-13 13:49