Coding study/python

24-10-01 class

rwg5565 2024. 10. 1. 14:05

예전 코드는 한번에 통으로 작동했는데 객체(object)제한 프로그래밍을 개발해 서로 영향도를 줄였다

객체(object) 장점:

프로그래밍 새로운 기능 추가 면에서 아주 쉽고 유지보수성이 좋다.

 

pass 잠시대기

 

 

파일 입출력

python 에서는 open() 함수를 사용하여 파일을 열고, 읽고, 쓰는 작업을 수행 할 수 있다.

 

# 파일 쓰기
with open('example.txt', 'w') as file:
    file.write('Hello, World!')

 

# 파일 읽기
with open('example.txt', 'r') as file:
    content = file.read()
    print(content)

 

여기서 open 파일을 여는 함수 r 읽기 w 쓰기 a는 추가모드이고 with 문을 사용하면 파일을 자동으로 닫아 준다.

 

데코레이터

데코레이터는 함수나 메소드를 수정하는 간단한 방법이다. 주로 코드의 중복을 줄이거나, 함수의 기능을 확장할 때 사용 된다.

 

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

say_hello()

 

@my_decorator는 say_hello 함수를 수정하는 데코레이터이다.

'Coding study > python' 카테고리의 다른 글

2024-10-09 Python 라이브러리로 데이터 분석하기  (1) 2024.10.09
2024-10-08 (class OOP 공부)  (0) 2024.10.08
2024-10-08 (for문 개념)  (0) 2024.10.08
2024-10-08 (if,elif,else)  (0) 2024.10.08
2024-10-07  (1) 2024.10.07