magic method

About_work·2022년 12월 28일
0

python 기초

목록 보기
6/56

개념

  • Magic method 개념
    • 더블 언더스코어로 시작해서, 더블 언더스코어로 끝나는 메서드
    • special method 라고 부르기도 함
    • 특별한 기능을 제공
  • 언제쓸까?
      • 인스턴스 생성시 자동으로 호출되는 init
      • 인덱싱 기능을 제공하고 싶을 때
      • 덧셈 연산자를 수행할 떄, 어떤 정해진 동작이 수행되도록 하려고 할 때
        - 아래는 기존 class 끼리 덧셈 연산자를 지원하지 않는다는 예시
class Stock:
    pass 

a = Stock()
b = Stock()
print(a +b)

Traceback (most recent call last):
  File "/Users/brayden/PycharmProjects/study/s01.py", line 7, in <module>
    print(a + b)
TypeError: unsupported operand type(s) for +: 'Stock' and 'Stock'
  • 함수도 객체
    • 파이썬에서 함수의 이름에 ()를 붙여주면 호출되는 이유는 매직 메서드 때문이다!
    • ()는 사실, 클래스 내에 정의된 __call__ 메서드를 호출하는 방법입니다.
    • 어떤 class(타입)의 객체가 있을 때, ()를 붙여주면 해당 클래스에 정의된 magic method인 __call__ 이 호출됩니다.
class MyFunc:
    def __call__(self, *args, **kwargs):
        print("호출됨")


f = MyFunc()
f()

'호출됨'
    • 사실, 파이썬 함수는 'function 클래스'의 인스턴스 객체입니다.
  • 점의 비밀
    • 파이썬에서 객체에 .을 찍으면 해당 객체에 접근할 수 있는 이유가 magic method 덕분입니다.
    • 변수가 어떤 객체를 binding 하고 있을 때 .을 찍으면, 클래스의 __getattribute__ 라는 이름의 magic method를 호출해줍니다.
class Stock:
    def __getattribute__(self, item):
        print(item, "객체에 접근하셨습니다.")


s = Stock()
s.data

data 객체에 접근하셨습니다.

종류

컨테이너 관리: collection과 반복

  • 컨테이너는 sequence(list와 tuple 등) + mapping(dictionary 등)

__getitem__(self, key)

  • 객체에서 [] 연산자를 사용하여 조회할때 동작을 정의합니다.
  • 예를들어 list[10]list.__getitem__(10)으로 동작합니다.
    키의 타입이 적절하지 않다면 TypeError에러를, 키가 인덱스를 벗어났을 경우는 IndexError를 던져야 합니다.

__setitem__(self, key, value)

  • 객체에서 [] 연산자를 사용해서 변수를 지정할때 동작을 정의합니다.
  • 예를들어 list[10] = 1list.__setitem__(10, 1)으로 동작합니다.

__delitem__(self, key)

  • del object[]를 사용하는 경우 동작을 정의합니다.

class 표현

__str__(self)

  • 인스턴스를 str() 으로 호출할 때 호출되며,
  • 태생적인 목적 자체가 인자를 ‘문자열화’해 반환하라는 것이다.
  • 평문 문자는 Universal Interface이기 때문에, 서로 다른 데이터 타입이 상호작용하는 좋은 인터페이스가 된다.
  • __str__ 의 본질적인 목적은 객체를 ‘표현’하는 것(representation)에 있다기보다는
  • 추가적인 가공이나 다른 데이터와 호환될 수 있도록 문자열화하는 데 있다고 하겠다.
  • str(3)
    • str은 입력 받은 객체의 문자열 버전을 반환
    • str은 사실 내장 함수가 아니고, 파이썬의 기본 내장 클래스
    • 사실 내장 str 클래스의 생성자 메소드를 실행하고, 그 인자로 3을 주는 것과 같다.
    • 어떤 객체 object에 str, repr 를 씌우면 해당 객체의 클래스에 정의되어 있는 __str__, __repr__ 메소드가 해당 객체에 실행되고, 두 메소드에 있는 코드를 실행한다.

__repr__(self)

  • 인스턴스를 repr() 으로 호출할 때 호출되며,
  • repr 은 representation(표현)의 약자로, 표현은 어떤 객체의 ‘본질’보다는 외부에 노출되는, 사용자가 이해할 수 있는 객체의 모습을 표현
  • __str__가 서로 다른 자료형 간에 인터페이스를 제공하기 위해서 존재한다면,
  • __repr__은 해당 객체를 인간이 이해할 수 있는 표현으로 나타내기 위한 용도이다.

그 외

  • add
    • +에 매핑되어있다.
  • mul
    • *에 매핑됨
  • sub
    • -에 매핑됨
  • doc
    • docstring을 출력하는 메서드
  • del
    • 객체가 없어질 때 호출되는 메서드
    • 내부에 있는 reference counter가 0이 되면 삭제됨
  • enter / exit
    • with 키워드를 통해 블럭에 진입 시, context manager와 함께 사용되는 magic method
      • enter: 진입할 때 실행되는 메서드
      • exit: 블럭이 끝날 때 실행하는 메서드
  • await
    • await 표현을 사용할 수 있는 객체를 만들 때 사용
    • iterator를 반환함
  • dict
    • class object의 attribute 정보를 확인하기 위해 사용
    • 딕셔너리 형태로 확인할 수 있다.
class Test:
	def __init__(self, name):
		self.name = name
		self.test_dict = {'a':1, 'b':2}
		self.test_list = ['1','2','3']
	
# Test 객체 생성		
test_object = Test("minimi")

# __dict__ 메소드를 이용해보면 type이 dict인 것을 확인 할 수 있다.
print(type(test_object.__dict__)) # <class 'dict'>

# print 해보면, 객체에 선언한 변수들이 key,value로 들어간 것을 확인할 수 있다.
print(test_object.__dict__)  # {'name': 'minimi', 'test_dict': {'a': 1, 'b': 2}, 'test_list': ['1', '2', '3']}

# dict 형태이기 때문에 key 값으로 조회시 바로 value를얻을 수 있다.
print(test_object.__dict__['name']) # minimi

# 번외 : dictionary의 key, value를 얻을 수 있는 items() 로 dict로 재변경
print(test_object.__dict__.items()) # dict_items([('name', 'minimi'), ('test_dict', {'a': 1, 'b': 2}), ('test_list', ['1', '2', '3'])])
print(type(test_object.__dict__.items())) # <class 'dict_items'>
object_dict = dict(x for x in test_object.__dict__.items()) # {'name': 'minimi', 'test_dict': {'a': 1, 'b': 2}, 'test_list': ['1', '2', '3']}
print(type(object_dict)) # <class 'dict'>
profile
새로운 것이 들어오면 이미 있는 것과 충돌을 시도하라.

2개의 댓글

comment-user-thumbnail
2023년 4월 17일

Magic method is computer programming that describes a special method in a class that performs a specific task. These methods are also known as "dunder methods" because their names start and end with double underscores. I say you can visit this Virtual magician for new tips of magic. Magic methods are used to define how objects of a class behave in specific situations, such as when they are created, compared, or printed. They are an essential part of object-oriented programming and help to make code more concise and readable.

답글 달기
comment-user-thumbnail
2023년 4월 26일

In computer programming, a magic method (also known as dunder methods) is a special method that starts and ends with double underscores, such as "init" and "str". Also, you check this fiocchi 209 primers for sale and learn more new ways for the latest primers products. These methods define how an object behaves in certain situations or operations, such as object initialization, printing, comparison, and mathematical operations. Magic methods are essential in creating custom objects and classes that behave like built-in objects in Python and other programming languages.

답글 달기