-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic_method_example_0.py
More file actions
56 lines (43 loc) · 1.06 KB
/
Copy pathmagic_method_example_0.py
File metadata and controls
56 lines (43 loc) · 1.06 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 클래스 안에 정의할 수 있는 빌트 인 메소드
# 모든 데이터 타입은 클래스
print(int)
print(list)
print(dict)
print(set)
# 클래스의 모든 속성 및 메소드 출력
print(dir(int))
print(dir(list))
# 빌트인 메소드를 이용
i = 10
print(i.__add__(100))
print(i.__bool__(), bool(i))
print(i.__mul__(7), i * 7)
# 클래스 예제 1
class Fruit:
def __init__(self, name, price):
self.name = name
self.price = price
def __str__(self):
return f"{self.name} is ${self.price}"
def __add__(self, other):
return self.price + other.price
def __sub__(self, other):
return self.price - other.price
def __le__(self, other):
if self.price <= other.price:
return True
else:
return False
def __ge__(self, other):
if self.price >= other.price:
return True
else:
return False
f1 = Fruit("apple", 10)
f2 = Fruit("orange", 5)
print(f1)
print(f2)
print(f1 + f2)
print(f1 - f2)
print(f1 >= f2)
print(f1 <= f2)