2022. 08. 16(화) Python 공부 12일차

Dylan·2022년 8월 16일
0
post-thumbnail

클래스 정의 및 사용하기

class란?

  • 실세계의 것을 모델링하여 속성(attribute)와 동작(method)를 갖는 데이터 타입
  • python에서의 string, int, list, dict... 모두가 다 클래스로 존재
  • 예를들어 학생이라는 클래스를 만든다면, 학생을 나타내는 속성과 학생이 행하는 행동을 함께 정의 할 수 있음
  • 따라서, 다루고자 하는 데이터(변수)와 데이터를 다루는 연산(함수)를 하나로 캡슐화(encapsulation)하여 클래스로 표현
  • 모델링에서 중요시 하는 속성에 따라 클래스의 속성과 행동이 각각 달라짐
### class란?

a = [1, 2, 3, 4]
a.append(5)
print(a)

object란?

  • 클래스로 생성되어 구체화된 객체(인스턴스)
  • 파이썬의 모든 것(int, str, list... etc)은 객체(인스턴스)
  • 실제로 class가 인스턴스화 되어 메모리에 상주하는 상태를 의미
  • class가 빵틀이라면, object는 실제로 빵틀로 찍어낸 빵이라고 비유 가능

class 선언하기

  • 객체를 생성하기 위해선 객체의 모체가 되는 class를 미리 선언해야 함
### class 선언하기
class Person:
    pass


bob = Person()
cathy = Person()

a = list()
b = list()

print(type(bob), type(cathy))
print(type(a), type(b))

생성자(init)의 이해 및 사용하기

__init__(self)

  • 생성자, 클래스 인스턴스가 생성될 떄 호출됨
  • self인자는 항상 첫번째에 오며 자기 자신을 가리킴
  • 이름이 꼭 self일 필요는 없지만, 관례적으로 self로 사용
  • 생성자에서는 해당 클래스가 다루는 데이터를 정의
    • 이 데이터를 멤버 변수(member variable) 또는 속성(attribute)라고 함
### __init__

class Person:
    def __init__(self):
        print(self, 'is generated')


p1 = Person()
p2 = Person()

# <__main__.Person object at 0x000001C69924CFD0> is generated
# <__main__.Person object at 0x000001C69924CF10> is generated

class Person:
    def __init__(self, name, age=10):
        # print(self, 'is generated')
        self.name = name
        self.age = age


p1 = Person('Bob', 30)
p2 = Person('Kate', 20)
p3 = Person('Aaron')

print(p1.name, p1.age)
print(p2.name, p2.age)
print(p3.name, p3.age)

# Bob 30
# Kate 20
# Aaron 10

self 키워드의 이해 및 사용하기

self

  • 파이썬의 method는 항상 첫번째 인자로 self를 전달
  • self는 현재 해당 메소드가 호출되는 객체 자신을 가리킴
  • C++ / C#, Java의 this에 해당
  • 역시, 이름이 self일 필요는 없으나, 위치는 항상 맨 처음의 parameter이며 관례적으로 self로 사용
### self

class Person:
    def __init__(self, name, age=10):
        print('self: ', self)
        self.name = name
        self.age = age

    def sleep(self):
        print('self: ', self)
        print(self.name, '은 잠을 잡니다.')


a = Person('Bob', 30)
b = Person('Kate', 20)

print(a)
print(b)

a.sleep()
b.sleep()

# self:  <__main__.Person object at 0x0000020AE3C3CEB0>
# self:  <__main__.Person object at 0x0000020AE3C3CDC0>
# <__main__.Person object at 0x0000020AE3C3CEB0>
# <__main__.Person object at 0x0000020AE3C3CDC0>
# self:  <__main__.Person object at 0x0000020AE3C3CEB0>
# Bob 은 잠을 잡니다.
# self:  <__main__.Person object at 0x0000020AE3C3CDC0>
# Kate 은 잠을 잡니다.

method, static method 정의 및 사용하기

method 정의

  • 멤버함수라고도 하며, 해당 클래스의 obejct에서만 호출 가능
  • 메소드는 객체 레벨에서 호출되며, 해당 객체의 속성에 대한 연산을 행함
  • {obj}.{method}() 형태로 호출됨
### method 정의

# 1. 숫자를 하나 증가
# 2. 숫자를 0으로 초기화

class Counter:
    def __init__(self):
        self.num = 0

    def increment(self):
        self.num += 1

    def reset(self):
        self.num = 0

    def print_current_value(self):
        print('현재값은: ', self.num)


c1 = Counter()
c1.print_current_value()
c1.increment()
c1.increment()
c1.increment()
c1.print_current_value()

c1.reset()
c1.print_current_value()

# 현재값은:  0
# 현재값은:  3
# 현재값은:  0

method type

  • instance method - 객체로 호출
    • 메소드는 객체 레벨로 호출 되기 때문에, 해당 메소드를 호출한 객체에만 영향을 미침
  • class method(static method) - class로 호출
    • 클래스 메소드의 경우, 클래스 레벨로 호출되기 때문에, 클래스 멤버 변수만 변경 가능
### method type
class Math:
    @staticmethod
    def add(a, b):
        return a + b

    @staticmethod
    def multiply(a, b):
        return a * b


print(Math.add(10, 20))
print(Math.multiply(10, 20))

# 30
# 200

클래스 상속의 이해 (코드를 재사용하기2)

Class Inheritance(상속)

  • 기존에 정의해둔 클래스의 기능을 그대로 물려받을 수 있다.
  • 기존 클래스에 기능 일부를 추가하거나, 변경하여 새로운 클래스를 정의한다.
  • 코드를 재사용할 수 있게된다.
  • 상속 받고자 하는 대상인 기존 클래스는 (Parent, Super, Base class라고 부른다.)
  • 상속 받는 새로운 클래스는(Child, Sub, Derived class 라고 부른다)
  • 의미적으로 is - a 관계를 갖는다.
### Class Inheritance(상속)

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def eat(self, food):
        print('{}은 {}를 먹습니다'.format(self.name, food))

    def sleep(self, minute):
        print('{}은 {}분동안 잡니다'.format(self.name, minute))

    def work(self, minute):
        print('{}은 {}분동안 일합니다'.format(self.name, minute))


class Student(Person):
    def __init__(self, name, age):
        self.name = name
        self.age = age


# Employee(부모클래스)
class Employee(Person):
    def __init__(self, name, age):
        self.name = name
        self.age = age


bob = Student('Bob', 25)
bob.eat('BBQ')
bob.sleep(30)
bob.work(60)

# Bob은 BBQ를 먹습니다
# Bob은 30분동안 잡니다
# Bob은 60분동안 일합니다

method override

  • 부모 클래스의 method를 재정의(override)
  • 하위 클래스(자식 클래스)의 인스턴스로 호출시, 재정의된 메소드가 호출됨
### method override

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def eat(self, food):
        print('{}은 {}를 먹습니다'.format(self.name, food))

    def sleep(self, minute):
        print('{}은 {}분동안 잡니다'.format(self.name, minute))

    def work(self, minute):
        print('{}은 {}분동안 일합니다'.format(self.name, minute))


class Student(Person):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def work(self, minute):
        print('{}은 {}분동안 공부합니다'.format(self.name, minute))

# Employee(부모클래스)
class Employee(Person):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def work(self, minute):
        print('{}은 {}분동안 업무를 합니다'.format(self.name, minute))


bob = Student('Bob', 25)
bob.eat('BBQ')
bob.sleep(30)
bob.work(60)

bob = Employee('Bob', 25)
bob.eat('BBQ')
bob.sleep(30)
bob.work(60)

# Bob은 BBQ를 먹습니다
# Bob은 30분동안 잡니다
# Bob은 60분동안 공부합니다

# Bob은 BBQ를 먹습니다
# Bob은 30분동안 잡니다
# Bob은 60분동안 업무를 합니다

super

  • 하위클래스(자식 클래스)에서 부모클래스의 method를 호출할 때 사용
### super

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def eat(self, food):
        print('{}은 {}를 먹습니다'.format(self.name, food))

    def sleep(self, minute):
        print('{}은 {}분동안 잡니다'.format(self.name, minute))

    def work(self, minute):
        print('{}은 {}분동안 준비를 합니다.'.format(self.name, minute))


class Student(Person):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def work(self, minute):
        super().work(minute)
        print('{}은 {}분동안 공부합니다'.format(self.name, minute))

# Employee(부모클래스)
class Employee(Person):
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def work(self, minute):
        super().work(minute)
        print('{}은 {}분동안 업무를 합니다'.format(self.name, minute))

bob = Employee('Bob', 25)
bob.eat('BBQ')
bob.sleep(30)
bob.work(60)

# Bob은 BBQ를 먹습니다
# Bob은 30분동안 잡니다
# Bob은 60분동안 준비를 합니다.
# Bob은 60분동안 업무를 합니다

다음시간에

special method

  • __로 시작 __로 끝나는 특수 함수
  • 해당 메소드들을 구현하면, 커스텀 객체에 여러가지 파이썬 내장 함수나 연산자를 적용 가능
  • 오버라이딩 가능한 함수 목록은 아래 링크에서 참조

0개의 댓글