arguments & Object Oriented Programming

arguments

  • *args
    • 정해지지 않은 수의 positional arguments를 함수에 전달할 수 있음
    • 하나의 tuple로 묶여서 전달됨
  • **kargs
    • 정해지지 않은 수의 keyword arguments를 함수에 전달할 수 있음
    • 하나의 dictionary로 묶여서 전달됨
def plus(*nums):
    result = 0
    for num in nums:
        result += num
    return result

print(plus(1, 2, 3, 5, 7, 28, 5489, 15))

Object Oriented Programming

  • class: 객체를 생성하기 위한 설계도
    • method: class 내부에 있는 function, 첫 번째 인자로 무조건 self를 받아야 함
    • property: class 내부에 있는 variable
    • dir(class): class의 모든 methods와 properties를 리스트로 확인할 수 있음
    • override: class의 method를 재정의하는 것
    • extends: 클래스 상속
  • instance: class로 생성한 객체
class Car():
    # instance를 생성하는 method
    def __init__(self, **kwargs):
        self.wheels = 4
        self.doors = 4
        self.windows = 4
        self.seats = 5
        self.color = kwargs.get('color', 'black')
        self.price = kwargs.get('price', '$20')

    # object를 string으로 바꾸는 method
    def __str__(self):
        return f'Car with {self.wheels} wheels'

    # 사용자 정의 method
    def go(self):
        print('Go')

# extend Car class
class Convertible(Car):
    # extend __init__ method
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.time = kwargs.get('time', 10)

    def take_off(self):
        return "taking off"

# Car class로 생성한 instance
mini = Car(color='white', price='$1000')
porche = Convertible(color='red', price='$99999', time='5')
mini.go()
porche.take_off()
print(porche.time)

※ 강의 링크: https://nomadcoders.co/python-for-beginners

'Clone Coding > Python으로 웹 스크래퍼 만들기' 카테고리의 다른 글

#4 Website with Flask  (0) 2022.07.05
#2 Building a Web Scrapper  (0) 2022.07.04
#1 Python Theory  (0) 2022.07.04

+ Recent posts