TIL no.9 Basic Python 3

이조은·2020년 7월 2일
0

TIL self-study

목록 보기
9/19

1. 클래스 & 오브젝트

클래스는 함수와 변수를 모아 놓은 것. 오브젝트는 클래스를 써서 만든 것. 쉽게 클래스는 빵틀, 오브젝트는 빵에 비유할 수 있다. 오브젝트는 인스턴스라고 하기도 한다.

class Person:
  def say_hello(self):
    print("안녕!")
    
p = Person() #Person이라는 클래스를 가지고 p라는 오브젝트를 만듬
p.say_hello() #안녕!
class Person:
  name = "워니"
  
  def say_hello(self):
    print("안녕! 나는 " + self.name) #만들어진 오브젝트에 변수를 활용해야 할 때 self라는 인자가 쓰인다.

p = Person()
p.say_hello() #안녕! 나는 워니
name 변수를 "워니"로 고정 시키지 않고 오브젝트 만들 때 새로 이름을 짓고 싶을 때

class Person:
  def __init__(self, name):
    self.name = name #init함수는 self를 첫 인자로 받고 Person에서 새롭게 쓸 변수들을 설정할 수 있다.
  
  def say_hello(self):
    print("안녕! 나는 " + self.name)

wonie = Person("워니")
michael = Person("마이클")
jenny = Person("제니")

wonie.say_hello() #안녕! 나는 워니
michael.say_hello() #안녕! 나는 마이클
jenny.say_hello() #안녕! 나는 제니
안녕이라고 하는 대상 자체도 바꿔보자

class Person:
  def __init__(self, name):
    self.name = name #init함수는 self를 첫 인자로 받고 Person에서 새롭게 쓸 변수들을 설정할 수 있다.
  
  def say_hello(self, to_name):
    print("안녕! " + to_name + "나는 " + self.name)

wonie = Person("워니")
michael = Person("마이클")
jenny = Person("제니")

wonie.say_hello("철수") #안녕! 철수 나는 워니
michael.say_hello("영희") #안녕! 영희 나는 마이클
jenny.say_hello("미지") #안녕! 미지 나는 제니
여기에 나이까지 추가해보자

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
  
  def say_hello(self, to_name):
    print("안녕! " + to_name + "나는 " + self.name)
    
  def introduce(self):
    ptiny("내 이름은 " + self.name + " 그리고 나는 " + str(self.age) + " 살이야") #age가 숫자 타입 변수이므로 숫자 타입을 문자열 타입으로 캐스팅 잊지 말기

wonie = Person("워니", 20)
wonie.introduce() #내 이름은 워니 그리고 나는 20 살이야

2.상속

공통된 클래스가 하나 있고, 그 밑에 세부 클래스를 만들고 싶을 때.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
  
  def say_hello(self, to_name):
    print("안녕! " + to_name + "나는 " + self.name)
    
  def introduce(self):
    ptiny("내 이름은 " + self.name + " 그리고 나는 " + str(self.age) + " 살이야")
    
class Police(Person): #Police는 Person을 상속하는 것
  def arrest(self, to_arrest):
    print("넌 체포됐다, " + to_arrest)

class Programmer(Person):
  def program(self, to_program):
    print("다음엔 뭘 만들지? 아 이걸 만들어야겠다: " + to_program)
    
wonie = Person("워니", 20)
jenny = Police("제니, 20)
michel = Programmer("마이클", 22)

wonie.introduce() #내 이름은 워니 그리고 나는 20 살이야
jenny.introduce() #내 이름은 제니 그리고 나는 21살이야 #여기서 jenny는 경찰이었다. Police에는 함수는 하나 밖에 없는데도 불구하고 introduce를 쓸 수 있었던 이유는 Police가 Person을 상속하기 때문이다. Police는 Person의 함수를 다 쓸 수 있다. 만약 상속을 안 했다면 Person의 코드를 Police 안에 쓴 것과 마찬가지이다.
jenny.arrest("워니") #넌 체포됐다, 워니
michael.program("이메일 클라이언트") #다음엔 뭘 만들지? 아 이걸 만들어야겠다: 이메일 클라이언트
wonie.arrest("제니") #error: 'Person' object has no attribute 'arrest'
wonie.program("제니") #error: 'Person' object has no attribute 'program'
jenny.program("이메일 클라이언트") #error:'Police' object has no attribute 'program'
michael.arrest("워니") #error: 'Programmer' jas no attribute 'arrest'

3. 패키지 & 모듈

패키지는 어떤 기능을 구현하는 모듈들의 합이다. 라이브러리는 파이썬에서 패키지와 같다. 그렇다면 모듈은? 코드를 잘 모아서 기능 하나를 구현해 놓은 파일.

profile
싱글벙글

0개의 댓글