Special Method

kkiyou·2021년 5월 29일
0

Python

목록 보기
10/12

Special Method

1. __new__

object.__new__(cls[, ...])

Called to create a new instance of class cls. new() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of new() should be the new object instance (usually an instance of cls).

Typical implementations create a new instance of the class by invoking the superclass’s new() method using super().new(cls[, ...]) with appropriate arguments and then modifying the newly-created instance as necessary before returning it.

If new() is invoked during object construction and it returns an instance or subclass of cls, then the new instance’s init() method will be invoked like init(self[, ...]), where self is the new instance and the remaining arguments are the same as were passed to the object constructor.

If new() does not return an instance of cls, then the new instance’s init() method will not be invoked.

new() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.

2. __init__

object.__init__(self[, ...])
initialize, 인스턴스를 선언할 때 메소드 내부에서 정의된 내용을 자동으로 호출된다.

0개의 댓글