Skip to content

Latest commit

 

History

History
73 lines (53 loc) · 3.4 KB

File metadata and controls

73 lines (53 loc) · 3.4 KB

Python Programming An Introduction to Computer Science

Chapter4. Objects and Graphics

사전지식을 위해 아래는 교재 외 자료 참조 source : Object-Oriented Programming (OOP) in Python 3 https://realpython.com/python3-object-oriented-programming/#what-is-object-oriented-programming-oop

(개요)

  • Object-Oriented Programming vs. procedural programming
  • class = form or questionnaire vs. instance(object) = answered questionnaire

(해석)

  • 그래픽에서 point, circle 등은 object이고, 그래픽은 class이고, 관계로 보면 point, circle 등은 class의 instance가 됨. ==> point, circle, line 등 각각을 class라고 하고, 여기에 argements가 입력되어 구체화된 object를 instance라고 하는 것으로 수정하여 해석함. 왜냐하면 point.getX() 는 x좌표를 반환하는데, circle.getX() 라는 것은 원에서 x좌표 하나를 반환하라는 이상한 method가 되어, class가 method를 결정한다는 정의에 부합하지 않음. ==> graphics.py 는 위 클래스를 포괄하는 모듈임.
  • point.draw(win)에서 draw는 point의 method이며, win은 arguments가 됨.
    ==> 특이한 점은, 책에서 win = GraphWin() 이라고 쓰면 그래픽 윈도우가 생성되는데, 변수 할당 명령( = ) 을 했는데, 실행이 된다는 점.

(기타)

  • 아래 (예시)에서, Dog를 Class를 정의하는 것처럼 쓴 것 보이는데, 아래 Class Attribute에서 상위범주를 mammal로 했다면, mammal이 class가 되고, Dog가 instance가 되는 것인지?

(예시) class Dog:

# Class Attribute   
species = 'mammal'   

# Initializer / Instance Attributes
def __init__(self, name, age):
    self.name = name
    self.age = age

====================================================================

reference

•What Is Object-Oriented Programming (OOP)? objects are at the center of the object-oriented programming paradigm, not only representing the data, as in procedural programming, but in the overall structure of the program as well.

•Classes in Python class just provides structure—it’s a blueprint for how something should be defined, but it doesn’t actually provide any real content itself. The Animal() class may specify that the name and age are necessary for defining an animal, but it will not actually state what a specific animal’s name or age is.

•Python Objects (Instances) While the class is the blueprint, an instance is a copy of the class with actual values, literally an object belonging to a specific class. It’s not an idea anymore; it’s an actual animal, like a dog named Roger who’s eight years old. Put another way, a class is like a form or questionnaire. It defines the needed information. After you fill out the form, your specific copy is an instance of the class; it contains actual information relevant to you.

•How To Define a Class in Python class Dog: pass

◦Instance Attributes class Dog:

# Initializer / Instance Attributes
def __init__(self, name, age):
    self.name = name
    self.age = age

◦Class Attributes class Dog:

# Class Attribute
species = 'mammal'

# Initializer / Instance Attributes
def __init__(self, name, age):
    self.name = name
    self.age = age

•Instantiating Objects when you create a new instance of the class, Python automatically determines what self is (a Dog in this case) and passes it to the init method.