본문 바로가기
Python/Web Scraping

[Python] #3.3 Methods part One (#코딩공부)

by 함께 공부해요 2020. 2. 29.

https://youtu.be/dQK55j3D6cQ


<복습>

https://wook-2124.tistory.com/48

 

[Python] #3.2 Intro to Object Oriented Programming (#코딩공부)

https://youtu.be/OiaXhERY3Kw <복습> https://wook-2124.tistory.com/46 [Python] #3.0 Django is AWESOME / #3.1 *args **kwargs (#코딩공부) https://youtu.be/ol3eYQlCaUM <복습> https://wook-2124.tistory.c..

wook-2124.tistory.com

 

<준비물>

https://repl.it/

 

Online IDE, Editor, and Compiler - Fast, Powerful, Free

Repl.it is a simple yet powerful online IDE, Editor, Compiler, Interpreter, and REPL. Code, compile, run, and host in 50+ programming languages: Clojure, Haskell, Kotlin (beta), QBasic, Forth, LOLCODE, BrainF, Emoticon, Bloop, Unlambda, JavaScript, CoffeeS

repl.it

 

<코드기록>

# function과 method의 차이
# method는 class안에 있는 function
class Car():
  wheels = 4
  doors = 4
  windows = 4
  seats = 4

  def start():
    print("What is method.")


# python은 method를 호출할 때 그 method의 instance를 # 첫번째 argument로 사용하기 때문에 
# 1 was given 이라고 오류가 뜸! (python에만 해당되는 규칙)
class Car():
  wheels = 4
  doors = 4
  windows = 4
  seats = 4

  def start():
    print("What is method.")

porche = Car()
porche.start()


# python은 그 method를 호출한 instance를 argument로 사용함
# porche(instance)가 method를 호출했으므로 내가 지정하지 않았어도 첫번째 argument가 자동으로 되는 것
class Car():
  wheels = 4
  doors = 4
  windows = 4
  seats = 4

  def start(method):
    print(method.instance)
    print("What is method.")

porche = Car()
porche.instance = "This is First instance"
porche.start()


# method는 아무렇게나 이름을 정해줘도 
# 첫번째 argument는 porche로 정해져있기 때문에 
# method argument인 porche를 써주면 오류없이 출력이 됨!
class Car():
  wheels = 4
  doors = 4
  windows = 4
  seats = 4

  def start(this_is_nothing):
    print(porche.instance)
    print("What is method.")

porche = Car()
porche.instance = "This is First instance"
porche.start()


# 내가 만든 모든 method(class안에 있는 function)의 첫번째 argument는
# 그 method를 호출한 instance가 됨
# 때문에 porche.start()라고 method를 부른뒤 
# () 안에 또 porche를 입력하니 2 was given 오류가 뜬 것
# () 안에 이미 porche가 있음!!
class Car():
  wheels = 4
  doors = 4
  windows = 4
  seats = 4

  def start(self):
    print(self.doors)
    print("What is method.")

porche = Car()
porche.instance = "This is First instance"
porche.start(porche)

# 정리하자면, class(설계도)로 만든 첫 번째 instance인 porche(산출물)가 
# method인 start()를 호출했기 때문에 start()의 첫 번째 argument가 된 것임 

1.  function과 method의 차이

function은 class에 속하지 않고 혼자서 기능하는 것이다.

 

method는 class안에 내제되어 있는 function이라고 생각하면 된다.


2. Python의 규칙

python은 class(설계도) 안에 있는 method(함수)를 호출할 때 - porche.start()

method를 호출한 instance(여기서는 porche)를 method의 첫 번째 argument(인자)로 자동적으로 사용한다.

 

때문에 porche.start()를 호출하면 function이 제대로 실행되서 터미널이 비어있는 것이 아니라

1 was given 이라고 한 개, 즉 porche라는 argument가 이미 주어졌다는 오류가 뜬다.

이것은 python에만 적용되는 규칙이다.

 

위에서 말한 것 처럼 python은 class의 method를 호출한 instance를, method의 첫 번째 argument로 사용한다.

 

porche(instance)가 porche.start()로 method(start)를 호출했으므로, 따로 지정하지 않아도 자동으로 method인 start()의 첫 번째 argument가 된다.

 

정리하자면, porche.start()는 print(method.instance)와 print("What is method.") 두 개를 출력하게 되는데, ()안에 method라고 적었더라도 method가 porche argument를 받기 때문에 결국 print(porche.instance)인 "This is First instance"를 출력하는 것이다.

 

한번 더 이해해보자, def start(this_is_noting)이라고 start() 안에 변수명을 아무렇게나 정해도 () 안에는 이미 porche라는 instance가 이미 def start() 라는 method의 첫 번째 argument로 지정되어서 porche.start()를 하면 자기 자신을 호출하게 된다.

 

self는 자기 자신을 칭할 때 쓴다. 보면 첫 번째 argument는 instance인 porche이고, porche = Car()로 지정해놨으니 class Car()안에 지정된 doors = 4가 출력되는 것을 알 수 있다.

 

porche.start()라고 method를 부른 뒤, () 안에 첫 번째 argument인 porche를 한번 더 입력하니 2 was given 오류가 떴다. 이것은 start() 안에 이미 argument인 porche가 있다는 반증이 된다.

 

정리하자면, class(설계도)로 만든 첫 번째 instance인 porche(산출물)가 method인 start()를 호출했기 때문에 start()의 첫 번째 argument가 된 것이다.

 

class = 설계도(blueprint)

method = class안에 있는 function(def)

instance = class로 만든 산출물, 복제품

argument = ()안에 속하는 인자(a, b, ... , 뭐든지 가능!)


※ 신종 코로나 바이러스 조심하세요!!!!

댓글