Python MIT OCW Lec8 : Object Oriented Programming

김재만·2023년 9월 20일
0

Python MIT OCW

목록 보기
8/9

배울 내용

  1. Object Oriented Programmin
  2. Class

1. Object Oriented Programming (OOP)

What is Object?

  • Obects are a data abstraction that captures
  1. an internal representation
    : through data attributes
  2. an interface for interacting with object
    : through methods (functions)
    : defines behaviors but hides implementation

What is OOP?

  • Everything in PYthon is an Object (and has a Type)
  • Can create new objects of some type
  • Can manipulate objects
  • Can destroy objects
    : explicitly using del or just "forget"

Advantages of OOP

  • Bundle data into packages together with procedures
  • Divide-and-conquer development
    : increased modularity
  • Classes make it easy to reuse the code

2. Classes

Creating and Using your own types

  • Creating the classes
    : defining the class name
    : defining class attributes
  • Using the class
    : creating new instances of object
    : doing operations on the instances (wiki : 객체 지향 프로그래밍(OOP)에서 인스턴스(instance)는 해당 클래스의 구조로 컴퓨터 저장공간에서 할당된 실체를 의미한다.)

What are Attributes?

  • Data and procedures that belong to the class

  • Data attributes
    : think of data that make up the class
    : ex) coordinate is made up of two numbers

  • Methods (procedural attributes)
    : functions that only work with this class
    : how to interact with the object?
    : ex) you can define a distance btw. two coordinate objects

  • The "." operator is used to access any attribute

    Class Defining

  • Format

    class Coordinate(object) : 
     def __init__ (self, x, y) : # special method, initialize some data attributes
       self.x = x
       self.y = y

    : object is class parent. This means that Coordinate is a Python object and inherits (상속, 나중에 배움) all its attributes
    : Coordinate is a subclass of object
    : object is a superclass ob Coordinate
    : self is parameter to refer to an instance of the class

    Creating Instance of a Class

  • See this code

 class Coordinate(object) : 
  def __init__ (self, x, y) : 
    self.x = x
    self.y = y

c = Coordinate(3, 4)
origin = Coordinate(0, 0)
print(c.x) # 3
print(origin.x) # 0

: Don't provide argument for self, Python does this automatically
: self goes to c
: x goes to 3
: y goes to 4

Defining Method

  • Python always passes the object as the first argument
    : Convention is to use self as the name of the first argumet of all methods
  • See this code
class Coordinate(object) : 
  def __init__ (self, x, y) : 
    self.x = x
    self.y = y
  def distance(self, other) : 
    x_diff_sq = (self.x - other.x) ** 2
    y_diff_sq = (self.y - other.y) ** 2
    return (x_diff_sq + y_diff_sq) ** 0.5

: other is another parameter to method (other Coordinate object)

Using Method

  • Using the class
    : Conventional way
class Coordinate(object) : 
  def __init__ (self, x, y) : 
    self.x = x
    self.y = y
  def distance(self, other) : 
    x_diff_sq = (self.x - other.x) ** 2
    y_diff_sq = (self.y - other.y) ** 2
    return (x_diff_sq + y_diff_sq) ** 0.5

c = Coordinate(3, 4)
zero = Coordinate(0, 0)
print(c.distance(zero))
  • This equivalent to...
class Coordinate(object) : 
  def __init__ (self, x, y) : 
    self.x = x
    self.y = y
  def distance(self, other) : 
    x_diff_sq = (self.x - other.x) ** 2
    y_diff_sq = (self.y - other.y) ** 2
    return (x_diff_sq + y_diff_sq) ** 0.5

c = Coordinate(3, 4)
zero = Coordinate(0, 0)
print(Coordinate.distance(c, zero))

Special Method

  • A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names.
    : Setting a special method to None indicates that the corresponding operation is not available. (Python official website)
  • __str__
    : Python calls the __str__ method when used with print on your class object
    : See this code
class Coordinate(object) : 
  def __init__ (self, x, y) : 
    self.x = x
    self.y = y
  def distance(self, other) : 
    x_diff_sq = (self.x - other.x) ** 2
    y_diff_sq = (self.y - other.y) ** 2
    return (x_diff_sq + y_diff_sq) ** 0.5
  def __str__(self) : 
    return "<" + str(self.x) + "," + str(self.y) + ">"

c = Coordinate(3, 4)
print(c) # <3,4>
__add__(self, other) -> self + other

__sub__(self, other) -> self - other

__eq__(self, ohter) -> self == other

__lt__(slef, other) -> self < other

__len__(slef) -> len(self)

__str__(self) -> print(self)
profile
Hardware Engineer가 되자

0개의 댓글