Python/OOP
-
-
-
-
-
__str__ vs __repr__Python/OOP 2022. 11. 27. 20:42
__str__ To customize the string representation of a class instance, the class needs to implement the __str__ magic method. Internally, Python will call the __str__ method automatically when an instance calls the str() method. class Person: def __init__(self, first_name, last_name, age): self.first_name = first_name self.last_name = last_name self.age= age def __str__(self): return f'이름: {self.fi..
-
객체Python/OOP 2022. 9. 17. 13:29
객체?! - 속성(변수)과 행동(메서드)으로 이루어진 존재 ex) 인스타그램 유저 객체 속성: 이메일 주소, 비밀번호, 친구목록 행동: 좋아요를 누르는 행동, 친구를 추가/삭제 하는 행동 변수 1. 인스턴스변수 - 인스턴스가 개인적으로 가지고 있는 속성 2. 클래스 변수 - 여러 인스턴스들이 공유하는 속성 # 인스턴스 변수 # # 클래스 이름의 첫번째 글자는 무조건 대문자로! class User: pass # 3개의 인스턴스는 다르다 user1 = User() user2 = User() user3 = User() # 인스턴스 변수 정의하기 ''' 인스턴스이름.속성이름(인스턴스변수) = "속성에 넣을 값" ''' user1.name = "yuna1" user1.email = "yunajoe@gmail.co..