Anuvicleo
Anuvicleo

Reputation: 107

Different memory address for same object

Why am I getting two different memory addresses for the same object?

https://i.sstatic.net/UPY7p.png

Upvotes: 2

Views: 263

Answers (1)

Poojan
Poojan

Reputation: 3519

  • your x inside class __init__ should give error. Your code is running because you are using IDE like syder/jupyter which store previously run code's result. If you restart your IDE and run same code again it will raise error name 'x' is not defined
  • If you want to reference x (object of class) use self to get that reference.
>>> class test:
...     def __init__(self, max):
...         print(self, "b")
...
>>> x = test(2)
<__main__.test object at 0x0000016A080052C8> b
>>> print(x,"a")
<__main__.test object at 0x0000016A080052C8> a

Upvotes: 2

Related Questions