ka373

파이썬 클래스 관련, 간단히 몇가지 정리 본문

09. 파이썬

파이썬 클래스 관련, 간단히 몇가지 정리

ka373 2019. 9. 28. 07:21

■ 파이썬은 객체지향을 지원하지만 완성도는 떨어짐(하단의 참고 도서 "파이썬 정복" 저자의 의견)

 

 

■ 클래스 이름 첫 자는 대문자를 쓰는 것이 관행임


■ 생성자
__init__


 파괴자
__del__

 

파이썬은 메모리 관리 자동화를 지원하므로, 잘 사용하지는 않음


 상속
class 클래스이름(부모):

 

자식 클래스에서 부모의 메서드 호출 시 super() 메서드로 부모를 구해서 호출

 

다중상속을 지원하지만 가급적 사용하지 않는 것이 바람직함


정보 은폐
파이썬은 공식적으로 정보 은폐를 지원하지 않음

 

(1) 다만, 멤버 이름을 __("_" 2개)로 시작하면 실제 이름을 _클래스명__멤버명으로 변환
eg) __second => _Time__second

이를 private처럼 사용한다고 함.

 

(2) 또한, protected처럼 사용하기 위해 또한,  _("_" 1개)를 사용한다고 함

 

(1), (2) 두 경우 모두 클래스 외부에서 직접 접근 가능했으나,

Visual Studio Code 기준, (1)의 경우 코딩 시 어느 정도 감춰 진 것은 느낄 수 있었음(밑의 그림 참고)

 

정보 은폐의 이점 중 하나가 프로그래밍의 편의성이라는 것을 생각하면, 

어느 정도 의미가 있다고 생각


공식적으로 정보 은폐를 지원하지 않더라도,

멤버를 외부에서 마음대로 조작하게 두는 것 보다는
게터(Getter)와 세터(Setter) 메서드를 정의하는 것이 보편적

 

 

 간단한 예제(자작): 

* 파이썬 클래스의 특징을 간단히 활용해 보기 위한 것으로,

실전 프로그래밍과는 차이가 있을 수 있음

 

 

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
31
32
33
34
35
36
37
38
39
40
41
class Polygon:
    _center_x = 1
    _center_y = 2
    def __init__(self, center_x_entered, center_y_entered):
        self._center_x = center_x_entered
        self._center_y = center_y_entered
    def area(self):
        print("Unable to calculate area: unknown polygon type")
 
class Rectangle(Polygon):
    __width = 0
    __height = 0
    def __init__(self, witdth_entered, height_entered):
        self.__width = witdth_entered
        self.__height = height_entered
    def area(self):
        print("The center of the rectangle is: (" +  str(super()._center_x) + ", " + str(super()._center_y) + ")")
        print("The area of the rectangle is: " + str(self.__width * self.__height))
 
class Triangle(Polygon):
    __width = 0
    __height = 0
    def __init__(self, witdth_entered, height_entered):
        self.__width = witdth_entered
        self.__height = height_entered
    def area(self):        
        print("The center of the triangle is: (" +  str(super()._center_x) + ", " + str(super()._center_y) + ")")
        print("The area of the triangle is: " + str(0.5 * self.__width * self.__height))
 
poly1 = Polygon(35)
rect1 = Rectangle(57)
tri1 = Triangle(34)
 
print()
rect1.area()
tri1.area()
 
print()
print("The x-coordinate of the center of the polygon (directly): " + str(poly1._center_x))
print("The x-coordinate of the center of the triangle (directly): " + str(tri1._center_x))  #like protected
print("The height of the rectangle (directly): " + str(rect1._Rectangle__height))   #like private
cs

 

 

실행 결과: 

 

The center of the rectangle is: (1, 2)
The area of the rectangle is: 35
The center of the triangle is: (1, 2)
The area of the triangle is: 6.0

The x-coordinate of the center of the polygon (directly): 3
The x-coordinate of the center of the triangle (directly): 1
The height of the rectangle (directly): 7

 

 

참고:

 

 

 

 

 

 

 

 

 

________________________________

참고 도서:

 

파이썬 정복 
초판발행 2018년 04월 02일 
지은이 김상형 
펴낸곳 한빛미디어(주)

 

 

참고 사이트:

 

https://brownbears.tistory.com/112 
https://lambda2.tistory.com/3

 

Comments