THE PYTHON SHOW - MINI BYTES - EPISODE 1
ALEXA: Hi Python , how are you?
PYTHON: Hi Alexa , I am fine … Thanks for having me in the show.
ALEXA: I wanted to know three concepts : method accessor , method mutuator , operator overloading.
PYTHON: Ok , I can explain you . I wanted to make the session interactive . So tell me the literal meaning.. I mean English meaning of those three words as I am one among many languages having syntax in readable format…enough of flaunting myself ?? over to you..
ALEXA: Err… accessor means accessing something…mutuator, will google search… seems to be mutuation or modifying.. operator overloading refers to overloading put weight too much above it can handle…
PYTHON: Ok good , seems to be perfect synonyms … but I wonder Alexa a amazon product , google searching :P
ALEXA: Yeah … yeah … google search is everybody’s right , even amazon is one among everybody – so I can very well use them :p
PYTHON: Ok , enough of chit chatting lets come back to topic..
Method accessor : Access a object method – but does not change the value of the object.
Here value of x has not changed . The changed value is assigned to new object (y).
Now you would have guessed what could method mutuator do.
ALEXA: Yes .. so simple .. it modifies the current object
PYTHON: Good , then give me a snippet of the concept..
ALEXA: Err.. I can give … but still for the betterment of viewers , it makes sense to listen from the GURU.
PYTHON: Ok .. good handling of the request .. :p
The reverse method mutates the existing object, in this case the list that myList
refers to. Once called, a mutator method can’t be undone. The change or mutation
is permanent until mutated again by some other mutator method.
Alexa: That’s nice to know .. ??
Python: Well , we will write a snippet utilizing both the concepts.
Alexa: Sure..
Python: Below is a snippet of code using both the concepts . Detailed code is as attached.
Result :
Alexa: That sounds perfect … so are we moving to next concept of operator overloading?
Python: Yeah .. what are you feeling tired , digesting the concepts ?
Alexa: No .. Not at all .. the concept is awesome…
Python: Cool … here operators like + , - , * are overloaded as per our convience.. When I say overload I modify the definition of them.
Alexa: Do you mean like mutuation..?
Python: Yes , kind of but its local to that class and not global.
Alexa: Can you please explain further?
Python: Yes , as below – we use self which means that values assigned by a object can be accessed only by those object.
Name of Dog1 can be accessed only by dog1 object and not by dog2 object… Hope this is clear.
Alexa: Yes , now I understand the importance of self keyword.
Python: Good , coming back to overloading concept.
You can use the operator (+) or its method definition (__add__) as below to get output.
Now this method definition can be used in dog class to understand the behaviour.
Hope this concept is clear.
Alexa: Thanks for all the explanations. It was a nice having you in this show . Meet you in next episode with challenging queries.
Python : Bye.. Anytime ??