+ 2
What is th value inside self variable?
I tried printing it by print(self) it gave o/p like: <__main__.class object at 0x...> please explain...thanks in advance
2 Answers
+ 3
"self" is not a variable like val = n, "self" is an object refers to the instance attributes and methods, see this:
class Class:
x = 10
def func(self) :
print(self.x)
# this will show you what inside self
print(dir(self))
ob = Class()
ob. func()
Output:
10
[__class__, __delatrr__, ..., func, x]
Note: "self" is not special is not more than convention you can use car! or whatever, but keep following the convention because your code may be less readable by other python programmers.
+ 1
Actually, the stuff that is being printed isn't exactly the value of self. It is the memory location of self (class).
As every data has a memory location glued to it, classes are no exception :)