객체지향 프로그래밍에서의 객체를 생성하기 위한 것
속성과 메서드를 가지고 있다.
일단 용어 정리!
- 네임스페이스: 객체를 인스턴스화 할 때 저장된 공간
- 클래스 변수: 클래스 명으로 직접 사용 가능, 객체보다 먼저 생성
- 인스턴스 변수: 객체마다 별도로 존재, 인스턴스를 생성한 후에야 사용 가능
클래스 선언
class Company:
# 클래스 변수(Company의 인스턴스끼리 공유 가능)
company_count = 0
def __init__(self, name):
self.name = name
Company.company_count += 1
def __del__(self):
Company.company_count -= 1
print(Company.__dict__) # 클래스 변수, 클래스 네임 스페이스
samsung = Company('SAMSUNG')
lg = Company('LG')
google = Company('Google')
apple = Company('Apple')
print(samsung.name, samsung.company_count)
print(lg.name, lg.company_count)
print(google.name, google.company_count)
print(apple.name, apple.company_count)
print(Company.company_count)
del lg
del google
print(Company.company_count)
"""
{'__module__': '__main__', 'company_count': 0, '__init__': <function Company.__init__ at 0x000001FC2C473F78>, '__del__': <function Company.__del__ at 0x000001FC2C47A0D8>, '__dict__': <attribute '__dict__' of 'Company' objects>, '__weakref__': <attribute '__weakref__' of 'Company' objects>, '__doc__': None}
SAMSUNG 4
LG 4
Google 4
Apple 4
4
2
"""
'Programming Language > Python' 카테고리의 다른 글
Python - 파일 읽기, 파일 쓰기 (0) | 2020.03.14 |
---|---|
Python - 모듈, 패키지 (0) | 2020.03.14 |
람다식 (0) | 2020.03.13 |
*args, **kwargs (0) | 2020.03.13 |
Set (0) | 2020.03.12 |