Why am I getting two different memory addresses for the same object?
Asked
Active
Viewed 154 times
2
-
1Nope it should give you error. Restart your spyder or whatever that ide is. – Poojan Nov 21 '19 at 02:16
-
1This code alone should raise a NameError. – Klaus D. Nov 21 '19 at 02:17
-
Do you already have a variable `x` in your namespace (i.e. in one of the first 56 runs you did)? It might be using that is why – Green Cloak Guy Nov 21 '19 at 02:19
-
@GreenCloakGuy how to overcome this? – Anuvicleo Nov 21 '19 at 02:22
-
Also please dont put image of your code. Others have to type whole thing to debug. Always paste code as text. – Poojan Nov 21 '19 at 02:30
1 Answers
2
- 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 errorname '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
- as you can see both
x
andself
inside__init__
have same address. - Some reference to look at : What __init__ and self do on Python?

Poojan
- 3,366
- 2
- 17
- 33