여러가지/Python

Day 17 - Class(클래스)

15June 2024. 4. 20. 21:17

● GIT URL : https://github.com/yooyijun15/Python/tree/main/Day17

 

Python/Day17 at main · yooyijun15/Python

Contribute to yooyijun15/Python development by creating an account on GitHub.

github.com

 

● 학습 내용

1) class 생성

class User:
    pass

user_1 = User()

user_1.name = 'June'
user_1.id = '001'
print(user_1.name)
print(user_1.id)

 

2) init 함수

class Car:
    def __init__(self, seats_num):
        self.seats = seats_num

my_car = Car(5)
print(f"My car has {my_car.seats} seats.")

self : 초기화되거나 생성되는 실제 객체

원하는 만큼 매개변수 추가 가능하다.

seats_num : 매개변수

객체 구성 시 전달된다.

(ex. My car has 5 seats.)

여기서 매개변수 seats_num와 seats의 이름이 다르다는 것을 주목할 필요가 있다.

위와 같이 클래스의 함수에서 생성된 변수가 뜨는 것을 확인할 수 있다.

 

예시)

특이한 점은 함수에 포함되지 않은 클래스의 'new user being created... '구문이 한번만 출력된다는 것이다.

 

[참고] Open Trivia DB

URL : https://opentdb.com/

 

Open Trivia DB

Free to use, user-contributed trivia questions!

opentdb.com