So, as a newbie in Python
, I am trying to understand it with the use of some examples. I'd like to understand it better, so I have made this:
class MyClass(object):
i = 123
def hot(self):
self.i = 345
return self.i
a = MyClass()
print a.hot()
1) Is it correct when I state that hot is a method
and therefore needs to be printed like a.hot()
, so with the parentheses in the end?
Because, I could do this too:
class MyClass2(object):
i = 123
def __init__(self):
self.i = 345
a = MyClass2()
print a.i
And it both would give me 345.
2) What is the reason I am not using parentheses here? Is it because MyClass2
has not a method with a specific name (hence, it's __init__
) so this makes it an entirely different method type?
I would really love some help, because it's quite confusing sometimes. I am trying to understand how __init__
and hot
differ, if they are both are seen as methods in Python. Why aren't we using parenthesis for both, then?
3) Also, I guess return
is only used when having a method with a real name (like hot), is that correct?