본문 바로가기
Hello, World!

MIT OpenCourseWare :: Decomposition, Abstractions, and Functions

by sun_HY 2022. 2. 2.

MIT 6.0001 Introduction to Computer Science and Programming in Python, Fall 2016

Instructor: Dr. Ana Bell

 

In this lecture, Dr. Bell discusses program structuring, functions, specifications, scoping, and the difference between the "return" and "print" keywords in Python.

 

thumbnail


4. Decomposition, Abstraction, and Functions

2022-02-02 내용 정리, 의역 多

더보기

4. Decomposition, Abstraction, and Functions

  • 지금까지는 파일을 열고 필요한 요소들을 코드로 써넣음
    • 명령, 반복 등
    • 특정한 파일에 써넣었음
    => small problems엔 좋았지만, 더 커진다면?
    • 문제가 커질수록 detail을 챙기기 어려워질 것
  • 좋은 코드를 짜려면, 프로그램에 functionality를 더해야 한다
    • 코드가 길어지는 것보다, 더 많은 것을 할 수 있는 것이 중요함
    • 내가 짠 좋은 코드가 다른 사람에게도 유용하게 쓰일 수 있다.
  • Good Programming
    • more code not necessarily a good thing
    • measure good programmers by the amount of functionality
    • introduce **functions**
    • mechanism to achieve **decomposition** and **abstraction**

 

  • example - a projector
    • 우리는 지금 당장 projector을 만들 수는 없지만, 어떻게 작동시키는지는 알고 있다.
    • 사용하기 위해서 구동 원리를 알 필요는 없다.
    • **Abstraction!**
  • 만약 아주 큰 이미지를 띄우고 싶다면?
    • like the one used in Olympics
    • 커다란 이미지를 띄울 프로젝터는 아주 비쌀 것이고, 그 한 번을 위해 만들기도 아깝다
    • => 그렇다면? 여러개의 작은 프로젝터가 쪼개진 영상을 띄울수록 할 수 있다.
    • 여러 개의 장치가 하나의 목적을 위해 움직인다
    • **Decomposition**
  • 이 개념은 프로그래밍에 적용해보자
    • decomposition은 프로그래밍에서 '구조를 세우는' 문제와 같다.
    • -> creating structure
    • 프로그래밍에서, decomposition을 위해 코드를 작은 module로 나눌 수 있다.
    • 각각의 모듈을 작은 일을 수행하고, 결과가 나타난다.
    • 모듈은 코드를 쪼개기 위해서 사용된다는 점에서도 의미가 있지만, 중요한 건 **reusable**하다는 점이다.
    • it keeps your codes **organized** and **coherent**
    • abstraction = suppressing details
    • 작은 작업을 수행하기 위한 코드를 짜고 나면, 여러 번 다시 작성할 필요가 없다.
    • 이후에 변수와 기능 등만 알고 나면, 굳이 그 내부를 다시 알 필요는 없다.
    • => 나 뿐만 아니라 다른 이들도!
  • hat example
    • one hat - should write the function
    • the other hat - use the function
  • function (definition)
    • has a name / parameters / docstring / body
    • and returns something
    • definition / call(이름과 요구되는 수만큼의 변수)
    • name : should be something descriptive *for the user
    • docstring : how we achieve abstraction (multi-line comment)
      • 변수와 기능, 반환값 등을 설명
  • in the function body
    • **return**
      • can be anything you want
      • tell Python what to return after executing everything
    • scope
      • completely separate environment
      • function을 call하면 각각의 환경에 맞게 가동하기 시작함
      • 결과 return 후에 그 환경에서 벗어나 main program으로
    • 프로그램이 시작되면 global scope 생성
    • 함수 호출 전까지는 안에서 무슨 일이 일어나는지 중요하지 않음
    • 함수 호출 시 new scope - new environment가 나타남
      • 가동 과정과 return값을 찾기 시작
    ★ function이 어떤 값을 return하면, 생성되었던 scope은 사라지고 안에 있던 모든 변수와 과정 또한 사라짐!
    • 만약 return값을 지정하지 않으면, 파이썬이 알아서 None값을 출력해줄것임
      • None is not a string, it's a special type
    if_even_with_return(3)  # False (어떤 역할을 하지 않고 단순히 값을 가지고 있을 뿐(just sitting there) , 값을 보려면?)
    print(if_even_with_return(3)) # <- print해야함!
  • in Python, everything's an object
    • even functions are objects
    • therefore, arguments can take on any type, even functions

 

  • python은 자신의 scope 안에서 변수를 찾지 못하면 다른 scope로 넘어가 변수를 찾는 것도 가능하다
    • global scope를 사용하는 것은 때로 유용할 수 있다.
728x90

'Hello, World!' 카테고리의 다른 글

firebase database - 더미 데이터 연결하기  (0) 2022.11.10
정보처리기사 필기 합격 수기 :: 2022 1회차 / 비전공자 문과생  (0) 2022.09.14
조합  (0) 2022.06.20
Dash or Underscore in URL?  (0) 2022.05.22
Hello, World!  (0) 2022.02.02