ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄/Python

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

μ„œμ•„λž‘πŸ˜ 2023. 12. 11. 23:06

 

 

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



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ν”„λ‘œ 이상 되기 λ•Œλ¬Έμ—, 이 λ©”μ„œλ“œλ“€λ§Œ μ΅μˆ™ν•΄μ§€λ©΄ μ½”λ“œλ₯Ό λ³Ό λ•Œ 크게 λΆˆνŽΈν•¨μ΄ μ—†μœΌμ‹€ κ²λ‹ˆλ‹€!