Welcome to part 2 of object oriented programming in Python. In this tutorial we will start from where we left in Object Oriented Programming part 1 and take the concept of OOPs a step further. So, let’s take a look at the program that we had developed:
class Program(): def__init__(self,*args,**kwargs): self.lang = input(“What Language?: “) self.version = float(input(“Version?: ”)) self.skill = input(“What skill level?: “) p1 = Program()
Now , in our next step we would define a new method with which we can update the object that we call the method for. So, we write the following method:
def upgrade(self): new_version=input(“what version?:”) print(“We have updated the version for”,self.lang) self.version=new_version
On adding this piece of code our program would look something like this:
class Program(): def__init__(self,*args,**kwargs): self.lang = input(“What Language?: “) self.version = float(input(“Version?: ”)) self.skill = input(“What skill level?: “) def upgrade(self): new_version=input(“what version?:”) print(“We have updated the version for”,self.lang) self.version=new_version p1 = Program()
Analysis of ugrade() method:
1. While defining the method we write it as
What’s important to note here is that it is not important to pass ‘self’ as an object. As a matter of fact, you won’t get any error if you don’t pass self but we put it there to denote that it is a part of the object.
2. ‘new_version’ can also be defined as ‘self.new_version’. The difference between the two is that the latter will be the attribute of the entire object where as the former will remain an attribute internal to the method. When upgrade() method is called, it will prompt the user to provide the value for the new version and that value will be assigned to new_version attribute.
3. The next statement is:
print(“We have updated the version for”,self.lang)
It prints “We have updated the version for” followed by the old version of the language. Notice that here, we make a call to self.lang which is not the attribute of this method however, it is an attribute of the object and so we can get away with it.
4. We then assign the value of new_version to self.version using:
which is the old version value defined in the __init__() method.
Execution of the program
Now, let’s have a look at how the statements are executed when we run the program:
1. The moment you execute the program, the __init__() program will prompt you with “What Language?:” statement. Here let’s say you respond with “Python”. This string “Python” will be assigned to self.lang.
2. The next statement of the __init__() function will be called now and you will be asked about “Version?:”. You say: “2.7”. The value “2.7” is assigned to self.version.
3. Now the __init__() function will prompt you to answer the third question “What skill level?:”. You can say “Beginner”. The value “Beginner” is assigned to self.skill.
4. To check whether the values have been assigned as expected you can type p1.lang, p1.version or p1.skill to check if the value that you provided has been assigned correctly or not.
5. Now to call the upgrade method type “p1.upgrade()”, you will now be taken to the upgrade() method and you will be prompted to answer (“what version?:”) you say “3.4”. You would now get a message “We have updated the version for Python”. If you look at the code, in __init__() function we have defined self.lang as
self.lang = input(“What Language?: “)
Now if you look at the upgrade() method, it says print(“We have updated the version for”,self.lang). It refers to self.lang which is an attribute of the object defined in __init__() method and when we execute the upgrade method it refers to self.lang attribute to print this statement so the last word of the message , i.e. “Python” is the value that we assigned to self.lang.
6. Now if you call p1.version you will see that it displays the value of 3.4. In step 2. We had assigned it a value of 2.7 and it step 5. It was changed to 3.4.
GET YOUR FREE PYTHON EBOOK!