LoginSignup
3
3

More than 5 years have passed since last update.

とある初期化のインデ..id

Posted at

タイトルに反して特にヤマなし。そげぶ。

前回の記事のコメントを見ていて、ふと「newでreturnするインスタンスとinitで渡されるself変数ってどういう関係なんだろう」という疑問が湧いてきた。

初期化時に、インスタンスidを確認する
>>> class D(object):
    def __new__(cls):
        ins = super().__new__(cls)
        print(id(ins))
        return ins
    def __init__(self):
        print(id(self))

>>> d = D()
4333861392
4333861392

>>> print(id(d))
4333861392

結論

_newでreturnするインスタンス = __init_で渡されるselfインスタンス = 生成されたインスタンス

当然といえば当然か。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3