Python > Class > 생성자
파이썬에서도 인스턴스 객체를 생성할 때 초기화 작업을 위해 생성자 메서드를, 메모리 해제 등의 종료 작업을 위해 소멸자 메서드를 지원하고 있다. 생성자 메서드는 인스턴스 객체가 생성될 때 자동으로 호출되며, 소멸자 메서드는 인스턴스 객체의 레퍼런스 카운터가 ‘0′이 될 때 호출된다.
__init__(self, *args, **kwargs)
# 참고: https://www.agiliq.com/blog/2012/06/understanding-args-and-kwargs/
What does *args do in a function definition? It recieves a tuple containing the positional arguments beyond the formal parameter list. So, args is a tuple. Since ‘args’ is a tuple, we could iterate over it.
Let’s use ** from inside the function call. For this we want a dictionary. Remember, while using * in the function call, we required a list/tuple. For using ** in the function call, we require a dictionary.
What ** did while being used in a function call? It unpacked the dictionary used with it, and passed the items in the dictionary as keyword arguments to the function.
So, ** unpacks the dictionary i.e the key values pairs in the dictionary as keyword arguments and these are sent as keyword arguments to the function being called. * unpacks a list/tuple i.e the values in the list as positional arguments and these are sent as positional arguments to the function being called.
What does * * kwargs mean when used in a function definition? With **kwargs in the function definition, kwargs receives a dictionary containing all the keyword arguments beyond the formal parameter list. Remember kwargs will be a dictionary.















