\u001b[0;34m()\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mprint\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mv2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mVar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 인스턴스 v2 생성\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mi_mem\u001b[0m \u001b[0;31m# 인스턴스 v2에는 아직 f() 호출이 안되어서 i_mem 멤버 없음 ==> 생성자의 필요성\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: Var instance has no attribute 'i_mem'"
]
}
],
"source": [
"print Var.c_mem # 클래스 객체를 통하여 클래스 멤버 접근\n",
"\n",
"v1 = Var() # 인스턴스 v1 생성\n",
"print v1.c_mem # 인스턴스를 통하여 클래스 멤버 접근\n",
"v1.f() # 인스턴스 멤버 i_mem이 생성됨\n",
"print v1.i_mem # 인스턴스 v1을 통하여 인스턴스 멤버 접근\n",
"\n",
"print\n",
"v2 = Var() # 인스턴스 v2 생성\n",
"print v2.i_mem # 인스턴스 v2에는 아직 f() 호출이 안되어서 i_mem 멤버 없음 ==> 생성자의 필요성"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![inheritance](images/instance2.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- \"인스턴스 이름.멤버 이름\"으로 멤버를 참조할 때 멤버의 검색 순서\n",
" - 1) 인스턴스 멤버\n",
" - 2) 인스턴스 멤버가 없다면 클래스 멤버"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100\n",
"100\n",
"\n",
"50\n",
"100\n",
"100\n"
]
}
],
"source": [
"print v1.c_mem # 인스턴스 v1을 통해 클래스 멤버 참조\n",
"print v2.c_mem # 인스턴스 v2를 통해 클래스 멤버 참조\n",
"\n",
"print\n",
"v1.c_mem = 50 # 인스턴스 이름 공간에 c_mem생성\n",
"print v1.c_mem # 인스턴스 v1을 통해 인스턴스 멤버 참조\n",
"print v2.c_mem # 인스턴스 v2을 통해 클래스 멤버참조 (인스턴스 멤버가 없으므로, 클래스 멤버 참조)\n",
"print Var.c_mem # 클래스 멤버참조"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![inheritance](images/instance3.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***\n",
"## 4 생성자와 소멸자\n",
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- \\_\\_init__: 생성자 메소드\n",
" - 객체가 생성될 때 자동으로 불리어지는 메소드\n",
" - self 인자가 정의되어야 함\n",
"- \\_\\_del__: 소멸자 메소드\n",
" - 객체가 소멸 (메모리에서 해제)될 때 자동으로 불리어지는 메소드\n",
" - self 인자가 정의되어야 함\n",
" - 개발자가 특별히 작성하지 않아도 될 메소드\n",
" - 이유: 파이썬에서는 메모리나 기타 자원들의 해제가 자동으로 되기 때문에\n",
"- [참고] \\_\\_ (연속된 두 개의 언더라인)의 의미: 예약된 이름"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 다음 코드에 대한 설명\n",
" - mylife = Life() 로서 인스턴스 mylife가 생성되는 순간 \\_\\_init__ 생성자 메소드 호출\n",
" - sleep(3)에 의해 3초간 sleep 상태\n",
" - 3초 이후 함수가 리턴됨 --> 로컬 변수가 메모리에서 해제됨 --> \\_\\_del__ 소멸자 메소드 호출"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Birthday Wed Nov 18 22:51:54 2015\n",
"Sleeping for 3 sec\n",
"Deathday Wed Nov 18 22:51:57 2015\n"
]
}
],
"source": [
"# _*_ coding:utf-8 _*_\n",
"from time import ctime, sleep\n",
"\n",
"class Life:\n",
" def __init__(self): # 생성자\n",
" self.birth = ctime() # 현재시간에 대한 문자열을 얻는다.\n",
" print 'Birthday', self.birth # 현재 시간 출력\n",
" def __del__(self): # 소멸자\n",
" print 'Deathday', ctime() # 소멸 시간 출력 \n",
"\n",
"def test():\n",
" mylife = Life()\n",
" print 'Sleeping for 3 sec'\n",
" sleep(3) #3초간 sleep(block)상태에 있음 (CPU 점유 못함)\n",
"\n",
"test()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 인자를 받는 생성자 호출 가능\n",
"- [참고] \\_\\_str\\_\\_: print 예약어나 str() 내장함수 호출에 대응되는 메소드"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"10\n"
]
}
],
"source": [
"class Integer:\n",
" def __init__(self, i):\n",
" self.i = i\n",
" def __str__(self):\n",
" return str(self.i)\n",
"\n",
"i = Integer(10)\n",
"print i\n",
"print str(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일
"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}