본문 바로가기
Python(Eng. ver)

15. Object-oriented programming and Class

by 곽정우 2024. 3. 18.

1. Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is one of the important programming paradigms used for designing and implementing software. This paradigm divides the program into independent entities called "objects" and organizes the program through interactions among these objects.

  • Noun: Variable
  • Verb: Function

Programming in this way constitutes object-oriented programming.

Procedural Programming

Procedural programming is a programming approach where code is structured according to a series of steps or procedures. These steps or procedures are typically divided into functions or subroutines, each performing a specific task. It primarily follows a sequential procedure of 'input - process - output', executing the code from top to bottom while processing data. Programming languages like C mainly adhere to procedural style.

 

Functional Programming

Functional programming is a paradigm where functions are given primacy in writing programs. Functions can be passed to other functions or returned from them, and complex tasks are performed by combining functions. There's an effort to minimize state changes and side effects. Functional languages include Haskell, Lisp, Clojure, and some other languages also support functional programming. Functional programming is conducive to parallel processing and state management, aiding in writing concise and stable code by combining functions.

 

2. Class

  • Object: An object is a model of real-world entities or abstract concepts in the real world. For example, objects could be a car, a person, a bank account, etc. Objects consist of data (attributes, state) and methods (behaviors, functions).
  • Class: Class is a template or blueprint for creating objects. It defines the common attributes and behaviors of objects and is used to create objects. For instance, a "car" class can define properties (color, speed) and methods (drive, stop) that all car objects would have.
  • Instance: Instance refers to an object created based on a class. A class can create multiple instances, and each instance has independent data and methods.

 

2-1.  Object (Instance) Creation

 

2-2.  Initialization of Object Attributes

 

2-3. Using Methods

 

3. Constructor

In Python, a constructor is a special method that is automatically called when an instance of a class is created. The constructor is responsible for initializing the object, setting up necessary attributes when the object is created. In Python, the constructor method is named __init__.

 

4. Methods

A method is a concept similar to functions used in object-oriented programming (OOP) but defined within a class and associated with a specific object. Methods are shared among all instances of the class and are used to define the behavior of objects or perform specific tasks.

 

4-1. Method Definition

 

4-2. Types of Methods

  • Instance Method: Manipulates the state of an object or performs a specific operation on an object. Most class methods are instance methods. The __init__ method shown in the example above is also an instance method.
  • Class Method: Operates at the class level and is shared among all instances of the class. Class methods are defined using the @classmethod decorator and use cls as the first parameter.
  • Static Method: Not related to a specific class or instance and can be called independently of the class or instance. Static methods are defined using the @staticmethod decorator.

 

5. Closure

클로저(Closure)는 프로그래밍 언어에서 중요한 개념 중 하나로, 함수와 그 함수가 참조하는 외부 변수(또는 자유 변수) 사이의 관계를 나타냅니다. 클로저는 함수의 내부에서 정의된 함수로, 내부 함수가 외부 함수의 변수에 접근할 수 있고, 외부 함수는 내부 함수를 반환할 수 있습니다. 이로 인해 함수와 그 함수가 참조하는 상태(변수)를 함께 저장하고 유지할 수 있습니다.

 

6. 데코레이터

데코레이터(Decorator)는 파이썬에서 함수나 메서드의 동작을 수정하거나 확장하기 위한 강력한 도구입니다. 데코레이터는 함수나 메서드를 래핑하거나 감싸서 추가 기능을 제공하며, 코드 재사용성과 가독성을 향상시킵니다. 데코레이터는 @ 기호를 사용하여 함수나 메서드 위에 적용됩니다.

'Python(Eng. ver)' 카테고리의 다른 글

17. Special Method  (0) 2024.03.19
16. Inheritance in Python  (0) 2024.03.19
14. Callbacks and Lambdas in Python  (0) 2024.03.18
13. Scope of Variable in Python  (0) 2024.03.18
12. User-Defined Function  (0) 2024.03.15