Hello my friends, today’s entry is going to be the second part about Object Oriented Programming with Python, if you haven't read the first part of this set please go to with this link, now let's create a table to summarize what we have so far of our Clock Class.
Now for this second part we are going to override some of the built-in Python methods on objects so we can use operators and some helpful functions. The first ones are those methods that will allow us to use the logical operators.
· Not equal
When we are using theses operators on the construction of conditions for flow control (if-else, while) statements, we can use objects as the variables in the comparisons, but is this methods are not coded in the Class of the object, the execution will crash, so let's start by coding this examples using our Clock Class template, for simplicity in the code the methods already described in the first part will not be showed in this part.
As you can see in this code, these methods usually uses the same format of double underscore, the keywords eq and ne mean equal and not equal, in the definition of these methods the self parameter must be provided as the first argument followed by a name variable referencing the other object passed for comparison, it is recommended to use other for readability. Then you need to return a Boolean type, this can be directly achieved by returning a comparison of the data that actually is being used for the purpose of comparison, in this case is the seconds instance variable, in the previous part was not clear why to use only one variable for the Clock representation instead of one variable for each of seconds, minutes and hours, now it is more clear because we can have a smaller code in these methods between others advantages, let's code a little using this methods for comparisons.
In this ways there is no need to use the public interface of the Class to access the seconds data for making comparisons, you can make these comparisons by just using these methods and the comparison operators, let's code the rest of these methods and operators
· Less than
· Less or equal than
· Greater than
· Greater or equal than
And their respective code
Let's do some basic examples with these
Pretty cool ha? If you go to the Python official documentation on operators, you will see the entire list of operators and how to code your objects to have compatibility with them, but sometimes the nature of your objects has no real meaning for some operators, for instance you can use the addition operator +, for integers but what about Clock instances? Well you can, but adding both seconds and produce a new object with this result, but what about division? Can you divide one Clock with other Clock? Does this question even has sense?, well before we get philosophical let's code the addition operator (__add__).
And let's try an example of this.
And that's it, we have covered enough for now, so try building your own Python Classes, I can give you some hints on prototypes, a Card Class and Deck Class is really useful for programming Card Games. , I hope you have found this entry helpful to you. So thanks for coming by my new entry and I hope you have enjoyed this as much as I enjoyed writing it, stop by the comments if you want to discuss about this. Don't forget to share this with your Python Peers. Cheers my friends.