Python (2) - Function & Class
2021. 5. 16. 17:33ㆍ[AI]/Data Engineering
New Stuff
Key Points
- Function
- Class
Learned Stuff
[Function]
- 반복적인 코딩을 수행할 때 함수로 설정하면 수월함
Example
def add_minus_and_multiply(a,b) :
return (a+b) * (a-b)
# Ex)
print(add_minus_and_multiply(5,10)
# returns -75
[Class]
- 객체를 생성할 때 쓰임
Visualization
Explanation
- Instance - Class 로 정의된 객체를 설정한 init method 에 맞게 호출하는 것
- Method - Class 내부에 설정한 함수
Example
class Person :
def __init__(self,name,age) :
self.name = name
self.age = age
def __del__(self) :
print("DELETED")
def show_info(self) :
print(f"Name : {self.name} , Age : {self.age}")
# 상속
class Gender(Person) :
def __init__(self,name,age,gender) :
self.name = name
self.age = age
self.gender = gender
def new_show_info(self) :
print(f"Name : {self.name}, Age : {self.age} , Gender : {self.gender}")
Output
a = Gender('Bob',15,'male') # a 라는 instance 생성
del a # a instance 삭제 및 'DELETED' 출력
a = Gender('Bob',16,'male')
a.show_info() # Name 과 Age 만 뜸
a.new_show_info() # Name , Age , Gender 다 뜸 (Person Class 를 상속 받음)
# Output
'''
DELETED
Name : Bob , Age : 16
Name : Bob, Age : 16 , Gender : male
'''
728x90
'[AI] > Data Engineering' 카테고리의 다른 글
Environment & Database (6) - Cloud Database / MongoDB (0) | 2021.05.16 |
---|---|
Python (1) - Python Basic Data Structure (0) | 2021.05.16 |
Web Scraping + Example (뉴스 기사 제목 스크래핑) (0) | 2021.05.16 |
Flask Tutorial(1) - Creating Website (0) | 2021.05.16 |
Flask Tutorial(2) - Render_Template (0) | 2021.05.16 |