Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 프로그래머스스쿨
- StringBuilder
- 파이썬
- 객체
- 메소드
- Dict
- 저장소
- GIT
- event
- docker
- Java
- AssertJ
- synchronized
- Docker Desktop
- 클래스
- 자바
- Python
- class
- JS
- thread
- 스프링부트
- SpringBoot
- SSL
- JavaScript
- 자바스크립트
- 배열
- array
- Swing
- join()
- c#
Archives
- Today
- Total
정리노트
[파이썬/python] [클래스] 특수 메소드 본문
print(객체)
이것만으로 객체안의 정보들을 알 수 있다면 편할 것이다.
__str__() 메소드를 class 안에 정의하면 가능하다.
class Count:
def __init__(self, num):
self.num = num
def __eq__(self, other):
return self.num == other.num
def __str__(self):
message = "담긴숫자: " + str(self.num) # 문자열과 합치기 위한 데이터타입 문자열 변환 함수str
return message
num1 = Count(10)
print(num1) // 담긴숫자: 10
객체를 연산할 수 있는 특수 메소드
예시) __eq__ 메소드 ( == 를 사용하는 메소드)
class Count:
def __init__(self, num):
self.num = num
def __eq__(self, other):
return self.num == other.num
num1 = Count(10)
num2 = Count(10)
if num1 == num2:
print("두 객체의 값이 같음")
#출력: 두 객체의 값이 같음
divmod(x, y) >>> __divmod__(self, y) / 실수나눗셈과 나머지
x + y >>> __add__(self, y) / 덧셈 # y는 위 예시에서 other
x - y >>> __sub__(self, y) / 뺄셈
x * y >>> __mul__(self, y) / 곱셈
x / y >>> __truediv__(self, y) / 실수나눗셈
x // y >>> __floordiv__(self, y) / 정수나눗셈 (몫만)
x % y >>> __mod__(self, y) / 나머지
x ** y >>> __pow__(self, y) / 지수
x << y >>> __lshift__(self, y) / 왼쪽 비트 이동
x >> y >>> __rshift__(self, y) / 오른쪽 비트 이동
x <= y >>> __le__(self, y) / 작거나 같다
x < y >>> __lt__(self, y) / 작다
x >= y >>> __ge__(self, y) / 크거나 같다
x > y >>> __gt__(self, y) / 크다
x == y >>> __eq__(self, y) / 같다
x != y >>> __neq__(self, y) / 같지않다
728x90
'프로그래밍 > Python' 카테고리의 다른 글
[파이썬/python] [집합/set] 기본원리 (0) | 2022.09.28 |
---|---|
[파이썬/python] [튜플] 기본원리 (0) | 2022.09.28 |
[파이썬/python] [클래스] 클래스변수 (0) | 2022.09.28 |
[파이썬/python] [클래스] private 변수 / 접근자와 설정자 (0) | 2022.09.28 |
[파이썬/python] [클래스] 객체생성, 기본원리 연습-정리 (0) | 2022.09.25 |