Python
- 컴퓨터의 CPU, RAM, SSD를 활용하기 위해서 학습
- 변수선언 : RAM 사용 문법 : 식별자(저장공간을 구분하는 문자열) : PEP20, PEP8(autopep8,flake8)
- 데이터타입 : RAM 효율적 사용 문법 : int, float, bool, str, list, tuple, dict, set
- 연산자 : CPU 사용 문법 : 산술, 비교, 논리, 할당, 멤버 ...
- 조건문, 반복문 : if, elif, else : while, for, break, continue, range() ...
- 함수 : 반복 사용되는 코드를 묶어서 코드 작성 실행 : def, return, scope(global), lambda ...
- 클래스 : 변수, 함수를 묶어서 코드 작성 실행 : 객체지향 구현 : class, self, special methods(생성자)
- 모듈 : 변수, 함수, 클래스를 파일로 묶어서 코드 작성 실행 : 확장자 .py
- 패키지 : 여러개의 모듈을 디렉토리로 묶어서 관리 : 버전정보 : import, from, as
- 입출력 : SSD 사용 문법 : RAM(object) > SSD(file), SSD(file) > RAM(object) : pickle
# python 철학
import this
# 형식에 맞게 바꾸어줌
# pip install autopep8
python autopep8 file_name.py
# pip install flake8
flake8 file_name.py
Class
- 변수, 함수를 묶어서 코드 작성 실행
- 객체지향 구현 : 실제세계를 모델링하여 프로그램을 개발하는 개발 방법론 : 협업이 용이함
- 함수 사용 : 함수선언(코드작성) > 함수호출(코드실행)
- 클래스 사용
- 클래스 선언(코드작성:설계도작성) > 객체생성(메모리사용:제품생산) > 메서드실행(코드실행:제품사용)
- 메서드 : 클래스안에 선언되는 함수
- class 식별자 : UpperCamelCase, PascalCase(0), snake_case(X:변수,함수)
# 1. 클래스선언 : 코드작성
# 예금계좌 : Acount클래스 : belance, deposit(), withdraw()
class Account :
balance = 0
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
# 2. 객체생성 : 메모리사용
acc1 = Account()
acc2 = Account()
#dir() : 객체 안에 들어있는 변수, 함수 목록 출력
dir(acc1)[-3:], dir(acc2)[-3:]
# (['balance', 'deposit', 'withdraw'], ['balance', 'deposit', 'withdraw'])
# 3. 메서드실행 : 코드실행
acc1.balance = 1000
acc1.balance, acc2.balance
# (1000, 0)
acc1.withdraw(600)
acc2.deposit(3000)
acc1.balance, acc2.balance
# (400, 3000)
# acc 객체 클래스 : Account : balance, withdraw(), deposit()
# acc 객체 데이터타입 : Account : 사용자정의 데이터타입
type(acc1) # __main__.Account
- 객체
# > 객체는 어떤 데이터타입(클래스)인지에 따라서 사용가능한 변수, 메서드가 달라집니다.
# data1 객체 데이터타입 : str
# data1 객체 클래스 : str : Upper(), lower(), join() ...
data1 = 'python'
type(data1) # str
dir(data1)[-3:] #['translate', 'upper', 'zfill']
# data2 객체 데이터타입, 클래스 : list
data2=[1,3,2]
type(data2) #list
dir(data2)[-3:] #['remove', 'reverse', 'sort']
# arr 객체 데이터 타입 : array
import numpy as np
arr = np.array([1,2,3])
type(arr),arr #(numpy.ndarray, array([1, 2, 3]))
# 현재 선언된 변수 초기화
%reset
# 변수, 함수는 둘다 객체입니다.
data = 1
def plus():
return 1+2
type(data),type(plus) #(int, function)
# 현재 선언된 객체 목록 출력
%whos
special methods
- 특별한 기능을 하는 메서드
- 앞뒤로 __가 붙은 메서드
- 대표적으로 생성자 메서드
1. __init__(self) : 생성자 메서드
# 생성자 메서드 : __init__(self)
# - 특별한 기능 : 객체 생성시 실행되는 메서드
# 클래스선언
class Account :
# 생성자 메서드 : 메서드에서 사용되는 변수의 초기값 설정하는 용도 사용
def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
# 객체생성 : 메모리사용 : 원자재사용
acc = Account(1000)
# 메서드호출 : 불량품 생산
acc.deposit(1000)
acc.balance #2000
2. __add__() : + 연산자에 대한 코드 정의
d1, d2 = 1, 2
d3, d4 = '3', '4'
type(d1), type(d2), type(d3), type(d4) # (int, int, str, str)
# 데이터 타입이 다르면 같은 연산자라도 다르게 연산 수행 됩니다.
d1 + d2, d3 + d4 # (3, '34')
# __add__ : +연산자에 대한 코드 정의
# dir(d1)
# int 클래스의 __add__() 메서드 호출
d1.__add__(d2) # 3
# dir(d3)
# str 클래스의 __add__() 메서드 호출
d3.__add__(d4) # '34'
class Number :
def __init__(self, data):
self.data = data
def __add__(self, obj):
self.data -= obj.data
return self.data
num1 = Number(3)
num2 = Number(2)
num1 + num2 # num1이 __add__ 호출, num2가 obj파라미터로 호출
# 1
'KT AIVLE School > 웹크롤링' 카테고리의 다른 글
ZigBang 원룸 매물 데이터 수집 (0) | 2024.09.20 |
---|---|
NAVER API (0) | 2024.09.20 |
403 문제가 생겼을 시 (0) | 2024.09.20 |
Web Crawling (0) | 2024.09.19 |
Web이란? (0) | 2024.09.19 |