반응형
class Stock:
market = "kospi"
if(__name__ == '__main__'):
# When class is define namespace is generated as class name
# All of method and variable info is save in namespace as dictionary
print('='*30)
print(dir())
print('='*30)
print(Stock.__dict__)
print('='*30)
# Instance is have own namespace
s1 = Stock()
s2 = Stock()
print(dir())
print('='*30)
print(id(s1))
print('='*30)
print(id(s2))
print('='*30)
print(s1.__dict__) # it will be return empty namespace
print('='*30)
print(s2.__dict__)
print('='*30)
# assign value on the instance variable.
s1.market = 'Kosdaq'
# add variable in the namespace
print(s1.__dict__)
# check variable
print(s1.market)
print(s2.market)
# if namespace is empty then they connect to class namespace
실행결과
$ py namespace.py
==============================
['Stock', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
==============================
{'__module__': '__main__', 'market': 'kospi', '__dict__': <attribute '__dict__' of 'Stock' objects>, '__weakref__': <attribute '__weakref__' of 'Stock' objects>, '__doc__': None}
==============================
['Stock', '__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 's1', 's2']
==============================
16818552
==============================
16818600
==============================
{}
==============================
{}
==============================
{'market': 'Kosdaq'}
Kosdaq
kospi
반응형
'Programming > Python' 카테고리의 다른 글
[Python-Crawling] 정부 혁신 제안 사이트 크롤링 - 1 (0) | 2019.11.07 |
---|---|
[Python-Basic] Class Data Type - 4(Self) (0) | 2019.11.05 |
[Python-Basic] Class Data Type - 3(Inheritance) (0) | 2019.11.05 |
[Python-Basic] Class Data Type - 3(Constructor) (0) | 2019.11.05 |
[Python-Basic] Class Data Type - 2(Instance) (0) | 2019.11.05 |