Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- Android
- GIT
- es6
- Python
- Spring
- MSSQL
- SQL
- NPM
- Java
- R
- mybatis
- table
- window
- plugin
- 공정능력
- IntelliJ
- Sqoop
- mapreduce
- Kotlin
- SSL
- Eclipse
- 보조정렬
- xPlatform
- react
- hadoop
- JavaScript
- vaadin
- SPC
- Express
- tomcat
Archives
- Today
- Total
DBILITY
python class 본문
반응형
내부 메써드 작성할 때 argument로 self는 기본인가?
__init__는 생성자, __del__은 소멸자겠지(?)
#######################################################################################
class Avenger:
def __init__(self, name="", role="", tribe=""): # initializer, constructor?
self.name = name
self.role = role
self.tribe = tribe
print(f'{self} initialization')
def __del__(self):
print(f'{self} Destructor called, {self} deleted.')
def to_string(self): # method
print(self.name, self.role, self.tribe)
########################################################################################
iron_man = Avenger()
iron_man.name = 'Robert John Downey Jr.'
iron_man.role = 'Tony Stark(Iron man)'
iron_man.tribe = 'Human(Insane or Genius)'
black_widow = Avenger('Scarlett Ingrid Johansson', 'Natasha Romanoff(Black Widow)', 'Human(Cold or Weak)')
print(iron_man.name, iron_man.role, iron_man.tribe)
print(black_widow.name, black_widow.role, black_widow.tribe)
iron_man.to_string()
black_widow.to_string()
del iron_man
del black_widow
print(iron_man)
실행결과 중 하단의 오류는 소멸자 호출 후에 객체를 출력해 봄.
<__main__.Avenger object at 0x00000221A9653FD0> initialization
<__main__.Avenger object at 0x00000221A9653F70> initialization
Robert John Downey Jr. Tony Stark(Iron man) Human(Insane or Genius)
Scarlett Ingrid Johansson Natasha Romanoff(Black Widow) Human(Cold or Weak)
Robert John Downey Jr. Tony Stark(Iron man) Human(Insane or Genius)
Scarlett Ingrid Johansson Natasha Romanoff(Black Widow) Human(Cold or Weak)
<__main__.Avenger object at 0x00000221A9653FD0> Destructor called, <__main__.Avenger object at 0x00000221A9653FD0> deleted.
<__main__.Avenger object at 0x00000221A9653F70> Destructor called, <__main__.Avenger object at 0x00000221A9653F70> deleted.
Traceback (most recent call last):
File "C:\Dev64\workspace\python_study\classandobject.py", line 36, in <module>
print(iron_man)
NameError: name 'iron_man' is not defined
Process finished with exit code 1
반응형
'python' 카테고리의 다른 글
python 사진 이미지 다루기(image open, resize) (0) | 2021.08.19 |
---|---|
python requests 헤더(header), 쿠키(cookie) 추가하기 (0) | 2021.08.19 |
python file rename, copy etc. (0) | 2021.08.19 |
python multi-threading ( 멀티쓰레드 ) (0) | 2021.08.17 |
python deal with time ( 시간 다루기 ) (0) | 2021.08.17 |
Comments