파이썬 μŠ€νŽ˜μ…œ λ©”μ„œλ“œμ˜ λͺ¨λ“  것(__init__, __str__, __len__ λ“±)

2023. 12. 11. 23:06Β·ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄/Python

 

 

νŒŒμ΄μ¬μ—μ„œ μŠ€νŽ˜μ…œ λ©”μ„œλ“œ(λ˜λŠ” 맀직 λ©”μ„œλ“œ)λŠ” 이쀑 μ–Έλ”μŠ€μ½”μ–΄λ‘œ λ‘˜λŸ¬μ‹ΈμΈ 이름을 κ°€μ§€λ©°, 객체의 νŠΉμ • λ™μž‘μ„ μ •μ˜ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€. μ΄λŸ¬ν•œ λ©”μ„œλ“œλ“€μ€ ν΄λž˜μŠ€μ— μ •μ˜λ˜λ©°, λ‚΄μž₯ ν•¨μˆ˜λ‚˜ μ—°μ‚°μž λ“±κ³Ό μƒν˜Έ μž‘μš©ν•˜λ„λ‘ μ„€κ³„λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. λ‹€μŒμ€ λͺ‡ κ°€μ§€ μ£Όμš” μŠ€νŽ˜μ…œ λ©”μ„œλ“œμ˜ μ’…λ₯˜μ™€ μ˜ˆμ‹œμž…λ‹ˆλ‹€.



1. __init__(self, ...): κ°μ²΄κ°€ μƒμ„±λ  λ•Œ ν˜ΈμΆœλ˜λŠ” λ©”μ„œλ“œλ‘œ, μ΄ˆκΈ°ν™”λ₯Ό λ‹΄λ‹Ήν•©λ‹ˆλ‹€.

   class MyClass:
       def __init__(self, x):
           self.x = x

   obj = MyClass(10)

 

2. __str__(self), __repr__(self): κ°μ²΄λ₯Ό λ¬Έμžμ—΄λ‘œ ν‘œν˜„ν•˜λŠ”λ° μ‚¬μš©λ˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

class MyClass:      
    def __init__(self, x):        
        self.x = x
 
    def __str__(self):   
        return "This is a MyClass object."    
    
    def __repr__(self):       
        return f"MyClass({self.x})"   
    
obj = MyClass(10)  
print(obj)   
print(repr(obj))


# This is a MyClass object.
# MyClass(10)

 

3. __len__(self): κ°μ²΄μ˜ κΈΈμ΄λ₯Ό λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œλ‘œ, λ‚΄μž₯ ν•¨μˆ˜ len()κ³Ό ν•¨κ»˜ μ‚¬μš©λ©λ‹ˆλ‹€.

  class MyList:
       def __init__(self, items):
           self.items = items

       def __len__(self):
           return len(self.items)

   my_list = MyList([1, 2, 3, 4])
   print(len(my_list))

 

4. __add__(self, other): κ°μ²΄ κ°„μ˜ λ§μ…ˆ μ—°μ‚°μ„ μ •μ˜ν•˜λŠ” λ©”μ„œλ“œλ‘œ, μ—°μ‚°μž + μ™€ ν•¨κ»˜ μ‚¬μš©λ©λ‹ˆλ‹€.

  class Point:
       def __init__(self, x, y):
           self.x = x
           self.y = y

       def __add__(self, other):
           return Point(self.x + other.x, self.y + other.y)

   p1 = Point(1, 2)
   p2 = Point(3, 4)
   p3 = p1 + p2

 

5. __getitem__(self, key), __setitem__(self, key, value): μΈλ±μ‹± λ° ν• λ‹Ήμ„ μ •μ˜ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

 class MyList:
       def __init__(self, items):
           self.items = items

       def __getitem__(self, index):
           return self.items[index]

       def __setitem__(self, index, value):
           self.items[index] = value

   my_list = MyList([1, 2, 3, 4])
   print(my_list[2])
   my_list[2] = 10

 

6. __eq__(self, other): 두 객체의 동등성을 κ²€μ‚¬ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€. == μ—°μ‚°μžμ™€ ν•¨κ»˜ μ‚¬μš©λ©λ‹ˆλ‹€.

   class MyClass:
       def __init__(self, x):
           self.x = x

       def __eq__(self, other):
           return self.x == other.x

   obj1 = MyClass(10)
   obj2 = MyClass(10)
   print(obj1 == obj2)

 

7. __lt__(self, other), __gt__(self, other): 객체 κ°„μ˜ μž‘μ€μ§€(<), 큰지(>) 비ꡐλ₯Ό μ •μ˜ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€.

 class Point:
       def __init__(self, x, y):
           self.x = x
           self.y = y

       def __lt__(self, other):
           return self.x < other.x and self.y < other.y

       def __gt__(self, other):
           return self.x > other.x and self.y > other.y

   p1 = Point(1, 2)
   p2 = Point(3, 4)
   print(p1 < p2)
   print(p1 > p2)

 

8. __enter__(self), __exit__(self, exc_type, exc_value, traceback): μ»¨ν…μŠ€νŠΈ 관리λ₯Ό μœ„ν•œ λ©”μ„œλ“œλ‘œ, with λ¬Έκ³Ό ν•¨κ»˜ μ‚¬μš©λ©λ‹ˆλ‹€.

class MyContext:
       def __enter__(self):
           print("Entering the context")
           return self

       def __exit__(self, exc_type, exc_value, traceback):
           print("Exiting the context")

   with MyContext() as context:
       print("Inside the context")

이 λ©”μ„œλ“œλ“€μ„ κ΅¬ν˜„ν•˜λ©΄ ν•΄λ‹Ή 클래슀의 μΈμŠ€ν„΄μŠ€λ₯Ό with λ¬Έμ—μ„œ μ‚¬μš©ν•  수 있으며, __exit__μ—μ„œ μ˜ˆμ™Έ 처리 등을 μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

 

9. __call__: 객체λ₯Ό ν•¨μˆ˜μ²˜λŸΌ ν˜ΈμΆœν•  수 μžˆλ„λ‘ ν•˜λŠ” λ©”μ„œλ“œμž…λ‹ˆλ‹€. 즉, ν•΄λ‹Ή 클래슀의 μΈμŠ€ν„΄μŠ€λ₯Ό 마치 ν•¨μˆ˜μ²˜λŸΌ ν˜ΈμΆœν•  수 있게 λ©λ‹ˆλ‹€. 이 λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ 객체가 호좜될 λ•Œ νŠΉμ • λ™μž‘μ„ μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

class CallableClass:
    def __call__(self, *args, **kwargs):
        print("Object is being called with arguments:", args)
        return sum(args)

# 객체λ₯Ό 생성
callable_obj = CallableClass()

# 객체λ₯Ό ν•¨μˆ˜μ²˜λŸΌ 호좜
result = callable_obj(1, 2, 3)
print("Result of the call:", result)

 

μœ„μ˜ μ˜ˆμ œμ—μ„œ `CallableClass`λŠ” `__call__` λ©”μ„œλ“œλ₯Ό μ •μ˜ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. μ΄ λ©”μ„œλ“œλŠ” κ°μ²΄κ°€ ν˜ΈμΆœλ  λ•Œ μ‹€ν–‰λ˜λ©°, ν˜ΈμΆœλœ μΈμžλ“€μ„ μΆœλ ₯ν•˜κ³  μΈμžλ“€μ˜ ν•©μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€.

`__call__`을 μ‚¬μš©ν•˜λ©΄ κ°μ²΄λ₯Ό μΌλ°˜μ μΈ ν•¨μˆ˜μ²˜λŸΌ μ‚¬μš©ν•  μˆ˜ μžˆμ–΄μ„œ, ν•¨μˆ˜μ™€ μœ μ‚¬ν•œ λ™μž‘을 ν•˜λŠ” κ°μ²΄λ₯Ό λ§Œλ“€ μˆ˜ μžˆμŠ΅λ‹ˆλ‹€. μ΄κ²ƒμ€ ν•¨μˆ˜λ₯Ό μΈμžλ‘œ λ°›κ±°λ‚˜ λ°˜ν™˜ν•˜λŠ” λ“±μ˜ μƒν™©μ—μ„œ μœ μš©ν•  μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.



이 외에도 λ‹€μ–‘ν•œ μŠ€νŽ˜μ…œ λ©”μ„œλ“œκ°€ 있으며, μ΄λŸ¬ν•œ λ©”μ„œλ“œλ“€μ„ 잘 ν™œμš©ν•˜λ©΄ 객체의 λ™μž‘μ„ μ»€μŠ€ν„°λ§ˆμ΄μ§•ν•  수 μžˆμŠ΅λ‹ˆλ‹€. κ·Έλ ‡μ§€λ§Œ μ§€κΈˆ μ†Œκ°œν•΄λ“œλ¦° μŠ€νŽ˜μ…œ λ©”μ„œλ“œμ˜ ν™œμš©λ„κ°€ 90ν”„λ‘œ 이상 되기 λ•Œλ¬Έμ—, 이 λ©”μ„œλ“œλ“€λ§Œ μ΅μˆ™ν•΄μ§€λ©΄ μ½”λ“œλ₯Ό λ³Ό λ•Œ 크게 λΆˆνŽΈν•¨μ΄ μ—†μœΌμ‹€ κ²λ‹ˆλ‹€!

 

 

 

μ €μž‘μžν‘œμ‹œ (μƒˆμ°½μ—΄λ¦Ό)

'ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄ > Python' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

파이썬 map, filter ν•¨μˆ˜(with. iterator)  (1) 2023.12.17
파이썬 리슀트 μ»΄ν”„λ¦¬ν—¨μ…˜, λ”•μ…”λ„ˆλ¦¬ μ»΄ν”„λ¦¬ν—¨μ…˜μ˜ λͺ¨λ“  것  (0) 2023.12.12
파이썬 μ˜ˆμ™Έμ²˜λ¦¬(Exception)의 λͺ¨λ“  것  (2) 2023.12.10
파이썬 λͺ¨λ“ˆκ³Ό νŒ¨ν‚€μ§€μ˜ λͺ¨λ“  것  (1) 2023.12.09
파이썬 μž…μΆœλ ₯(ν‘œμ€€, 파일 λ“±)의 λͺ¨λ“  것  (2) 2023.12.05
'ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄/Python' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • 파이썬 map, filter ν•¨μˆ˜(with. iterator)
  • 파이썬 리슀트 μ»΄ν”„λ¦¬ν—¨μ…˜, λ”•μ…”λ„ˆλ¦¬ μ»΄ν”„λ¦¬ν—¨μ…˜μ˜ λͺ¨λ“  것
  • 파이썬 μ˜ˆμ™Έμ²˜λ¦¬(Exception)의 λͺ¨λ“  것
  • 파이썬 λͺ¨λ“ˆκ³Ό νŒ¨ν‚€μ§€μ˜ λͺ¨λ“  것
μ„œμ•„λž‘πŸ˜ƒ
μ„œμ•„λž‘πŸ˜ƒ
Just Do ItπŸ’ͺ
  • μ„œμ•„λž‘πŸ˜ƒ
    G-Stack
    μ„œμ•„λž‘πŸ˜ƒ
  • 전체
    였늘
    μ–΄μ œ
    • 전체보기 (144)
      • ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄ (78)
        • C++ 기초 (28)
        • C++ μ‘μš© (18)
        • Python (18)
        • JavaScript & NodeJS (0)
        • Go (12)
        • React & NextJS (2)
        • Java (0)
      • AI (2)
      • 컴퓨터 ꡬ쑰 & 운영체제 (31)
      • μ•Œκ³ λ¦¬μ¦˜ (12)
      • λ°μ΄ν„°λ² μ΄μŠ€ (5)
      • λ„€νŠΈμ›Œν¬ (3)
      • λ””μžμΈνŒ¨ν„΄ (5)
      • μ„œλΉ„μŠ€ & 툴 (7)
      • νŠΈλ Œλ“œ&이슈 (1)
  • λΈ”λ‘œκ·Έ 메뉴

    • ν™ˆ
    • νƒœκ·Έ
    • λ°©λͺ…둝
  • 링크

  • 곡지사항

    • GμŠ€νƒμ˜ 기술 λΈ”λ‘œκ·Έ
  • 인기 κΈ€

  • νƒœκ·Έ

    μŠ€νƒ
    가상메λͺ¨λ¦¬
    λ””μžμΈνŒ¨ν„΄
    포인터
    컴퓨터
    fork
    반볡문
    Thread
    μž¬κ·€
    pointer
    λ°°μ—΄
    c++
    init
    λ©”λͺ¨λ¦¬
    νŒŒμΌμž…μΆœλ ₯
    νŒ¨ν‚€μ§€
    λ³€μˆ˜
    component
    RAM
    c
    λ°μ΄ν„°λ² μ΄μŠ€
    파이썬
    쑰건문
    상속
    go
    μ•Œκ³ λ¦¬μ¦˜
    ν•¨μˆ˜
    STD
    cpu
    ν•˜λ“œλ””μŠ€ν¬
  • 졜근 λŒ“κΈ€

  • 졜근 κΈ€

  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.6
μ„œμ•„λž‘πŸ˜ƒ
파이썬 μŠ€νŽ˜μ…œ λ©”μ„œλ“œμ˜ λͺ¨λ“  것(__init__, __str__, __len__ λ“±)
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”