Reputation: 59
I am learning Python, and I'm trying to simulate a card game. I have a question regarding dot notation. I have looked all over the internet for a specific answer but was unable to find one.
Why is it, that sometimes we are taught to call a method in dot notation like this:
object.methodName()
While other times we are shown to call it like this:
className.methodName(object)
What is the difference?
Here is a specific example. This is a method definition from the book (How to think like a Computer Scientist)
class Hand(Deck):
def __init__ (self, name = " "):
self.cards = []
self.name = name
def __str__(self):
s = "Hand "+ self.name
if self.isEmpty():
return s+" is empty\n"
else:
return s+ " contains\n" + Deck.__str__(self)
Sometimes the object comes before the method:
self.isEmpty()
Sometimes the object comes after the method, in the parenthesis:
Deck.__str__(self)
Which method do we use? Does it matter?
Upvotes: 3
Views: 3052
Reputation: 24119
Wenn you use:
object.methodName()
then you calling the method of a special instance with it's own data. As the method is related to the given instance you get different results.
a = Hand()
b = Hand()
then
a.cards
b.cards
may be different (in most cases).
To access this data the object self has to put in every method. This is done by:
def foo(self):
return self.cards
When you define the method.
Unless you don't write a special class method than you always have to put the self there.
When calling object.methodName()
the method automatically gets the information of the object it belongs to (because you hand the object/instance reference over before the dot. This information is stored in the parameter self
. Thus the method can for example do: return self.cards
But if you call it like Deck.__str__(self)
than you only have the class this means there is no self
(which is the object reference itself).
So the method does not know which instance data it belongs to, therefore you have to hand over the self
.
So summed up, if you call:
object.__str__()
the information about the instance is given because you used the object reference to call the method.
But if you use Deck
resp. the className
than you have to put the information about the instance also via className.method(self)
because otherwise there is no object reference for an object instance.
Upvotes: 1
Reputation: 59
Generally, the object comes first.
Like this:
object.methodName()
Or like this:
self.isEmpty()
But I wanted to know why my Python book is teaching me to call this one method like this:
Deck.__str__(self)
I wanted to know why "self" is in parentheses at the end, instead of at the beginning of the method. In other words, why not write it like this:
self.Deck._str_()
The reason, is that the code above would throw an error:
"Hand instance has no attribute Deck
The above error is caused because written this way, Python thinks that "Deck" is an attribute of self. But in this case it is not. Deck, is the PARENT type to the Hand type, it is not an attribute of self.
Here is the code block where you can see the code in question, as well as the parent type (Deck) and the child type I am working with (Hand). In this code, we are writing a method to overwrite the string formatting for Hand, by accessing the string formatting in Deck. The code snippet Deck.__str__(self)
is written this way because we need a way to tell Python which type object we would like it to copy the string formatting from. In this case, we wanted the string formatting from Deck, the parent.
class Hand(Deck):
def __init__ (self, name = " "):
self.cards = []
self.name = name
def __str__(self):
s = "Hand "+ self.name
if self.isEmpty():
return s+" is empty\n"
else:
return s+ " contains\n" + Deck.__str__(self)
Also, as stated by @ Martijn Pieters, using SUPER: super().__str__()
is better than doing it the way I did: Deck.__str__(self)
We have not yet learned Super, but you can learn more about it here(https://docs.python.org/2/library/functions.html#super). Super basically tells Python to find the parent formatting for you, and use that. Much easier!
I hope that this explanation is understandable. I tried to over-explain to make up for anything that is not clear :-/
Upvotes: 0
Reputation: 767
Sometimes the object comes after the method, in the parenthesis:
Deck.__str__(self)
No, the object always comes before its own method. There are two python "objects" in this line. You are passing the self
object (which is really a class instance) as a parameter to the __str__
method of the Deck
object (which is really a class).
Upvotes: 1