This week, I will give some examples of the object-oriented programming part of Python. The object-oriented programming has many advantages. And I really like them due to the reusability of the code and the feeling of modeling the world :-). I will give some examples that I usually use in my code as a quick guide of how to use these in your code. This tutorial serves as a quick guide, if you want to learn more, you should go to the documentation.
Define a class
Let's start to look at the following examples, where we create a People class.
class People(object):
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Greetings, " + self.name)
Typical python class has a constructor, that is the _init_() function, which initializes the class when you call it. This means that, when you first initialize your class, this function will be only run once. The 'self' means the instance itself, you can find a very good explanation here. The self.name and self.age are the attributes that the class will have, you can see from the following example. And the greet function is the method that we define in the class. Let's see how we can actually use it.
person1 = People(name = 'Iron Man', age = 35)
person1.greet()
print(person1.name)
Greetings, Iron Man
Iron Man
Inheritance
One of the most important features of using the object-oriented programming is that we can easily reuse the code above to create some new classes. For example, we want to have another class Teacher that have all the attributes and methods People class have, but at the same time, we want to have more new methods.
class Teacher(People):
def teach_students(self, x, y):
print('x + y = %d'%(x+y))
teacher1 = Teacher(name = 'Susan', age = 24)
teacher1.greet()
teacher1.teach_students(x = 3, y = 5)
Greetings, Susan
x + y = 8
We can see from the above code that, we don't need to re-define all the attributes and greet function, the Teacher class actually have all these from People class, this is due to this line: class Teacher(People), which basically say that we want Teacher class to get all the things from People class. This is called inherit, and class Teacher inherits from class People. And People is the parent class and Teacher is a child class. And then we could extend the methods in class Teacher by just define new functions. Or if we want to replace some of the old methods in People, all we need to do is to re-define the function, for example, in the following lines, we replace the greet method in People with a new one that greet the teacher.
class Teacher(People):
def greet(self):
print("Greetings, teacher: " + self.name)
def teach_students(self, x, y):
print('x + y = %d'%(x+y))
teacher1 = Teacher(name = 'Susan', age = 24)
teacher1.greet()
Greetings, teacher: Susan
The super method
Also, often times, we want to expand the constructor by having more attributes and so on, but at the same time, we don't want to re-type all the code as before, therefore, we could use the super method to avoid referring to the parent class explicitly. Let's see below that we want to add a studentId field into the Student class.
class Student(People):
def __init__(self, name, age, studentId):
super().__init__(name, age)
self.studentId = studentId
student1 = Student(name = 'Kevin', age = 20, studentId = '12345')
print('Student %s has id as %s'%(student1.name, student1.studentId))
Student Kevin has id as 12345
Multiple inheritances
What if we have a student_teacher class that we want to inherit from both Teacher and Student class. Easy, you can just do the following:
class Student_Teacher(Teacher, Student):
pass
st1 = Student_Teacher(name = 'Kate', age = 23, studentId = '54321')
print('Teacher %s has studentId as %s'%(st1.name, st1.studentId))
st1.teach_students(3,6)
Teacher Kate has studentId as 54321
x + y = 9
Great post full of useful tips! My site is fairly new and I am also having a hard time getting my readers to leave comments. Analytics shows they are coming to the site but I have a feeling "nobody wants to be first"Jogo para criança online
ReplyDeleteplay Games friv
free online friv Games
ReplyDeleteThank you for sharing wonderful information with us to get some idea about that content.
Python Institute in Hyderabad
Python Online Training in Hyderabad
Python Training in Hyderabad
Python Training
Python Online Training
Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
ReplyDeletePython Classes in Magarpatta
Great Post, Thanks for sharing the information.
ReplyDeletePython Online Training
Python Online Training in Chennai
Python Online Course in Chennai
Am here to testify what this great spell caster done for me. i never believe in spell casting, until when i was was tempted to try it. i and my wife have been having a lot of problem living together, she will always not make me happy because she have fallen in love with another man outside our relationship, i tried my best to make sure that my wife leave this woman but the more i talk to her the more she makes me fell sad, so my marriage is now leading to divorce because she no longer gives me attention. so with all this pain and agony, i decided to contact this spell caster to see if things can work out between me and my wife again. this spell caster who was a man told me that my wife is really under a great spell that she have been charm by some magic, so he told me that he was going to make all things normal back. he did the spell on my wife and after 5 days my wife changed completely she even apologize with the way she treated me that she was not her self, i really thank this man his name is Dr ose he have bring back my wife back to me i want you all to contact him who are having any problem related to marriage issue and relationship problem he will solve it for you. his email is oseremenspelltemple@gmail.com he is a man and his great. wish you good time.
ReplyDeleteHe cast spells for different purposes like
(1) If you want your ex back.
(2) if you always have bad dream
(3) You want to be promoted in your office.
(4) You want women/men to run after you.
(5) If you want a child.
(6) You want to be rich.
(7) You want to tie your husband/wife to be yours forever.
(8) If you need financial assistance.
(9) HIV/AIDS CURE
(10) is the only answer to that your problem of winning the lottery
Contact him today on oseremenspelltemple@gmail.com or whatsapp him on +2348136482342
Five weeks ago my boyfriend broke up with me. It all started when i went to summer camp i was trying to contact him but it was not going through. So when I came back from camp I saw him with a young lady kissing in his bed room, I was frustrated and it gave me a sleepless night. I thought he will come back to apologies but he didn't come for almost three week i was really hurt but i thank Dr.Azuka for all he did i met Dr.Azuka during my search at the internet i decided to contact him on his email dr.azukasolutionhome@gmail.com he brought my boyfriend back to me just within 48 hours i am really happy. What’s app contact : +44 7520 636249
ReplyDeleteMy name is Tom cam!!! i am very grateful sharing this great testimonies with you all, The best thing that has ever happened in my life, is how i worn the Powerball lottery. I do believe that someday i will win the Powerball lottery. finally my dreams came through when i contacted Dr. OSE and tell him i needed the lottery winning special numbers cause i have come a long way spending money on ticket just to make sure i win. But i never knew that winning was so easy with the help of Dr. OSE, until the day i meant the spell caster testimony online, which a lot of people has talked about that he is very powerful and has great powers in casting lottery spell, so i decided to give it a try. I emailed Dr. OSE and he did a spell and gave me the winning lottery special numbers 62, and co-incidentally I have be playing this same number for the past 23years without any winning, But believe me when I play the special number 62 this time and the draws were out i was the mega winner because the special 62 matched all five white-ball numbers as well as the Powerball, in the April 4 drawing to win the $70 million jackpot prize...… Dr. OSE, truly you are the best, with Dr. OSE you can will millions of money through lottery. i am a living testimony and so very happy i meant him, and i will forever be grateful to him...… you can Email him for your own winning special lottery numbers now oseremenspelltemple@gmail.com OR WHATSAPP him +2348136482342
ReplyDeleteI've gotten back with my ex boyfriend with the help of Dr.jumba, the best spell caster online and I highly recommend Dr.jumba to anyone in need of help!.. I want to testify of how i got back my boyfriend after he breakup with me, we have been together for 3 years, recently i found out my boyfriend was having an affair with another Girl, when i confronted him, it led to quarrels and he finally broke up with me, i tried all i could to get him back but all to no avail until i saw a post in a relationship forum about a spell caster who helps people get back their lost love through Love spell, at first i doubted it but decided to give it a try, when i contacted this spell caster via his email, and he told me what to do and i did it, Then he did a Love spell for me. 28 hours later, my boyfriend really called me and told me that he misses me so much, So Amazing!! So that was how he came back that same day, with lots of love and joy, and he apologized for his mistake, and for the pain he caused me. Then from that day, our relationship was now stronger than how it were before, All thanks to DR.Jumba. he is so powerful and i decided to share my story on the Internet that Dr.Jumba real and powerful spell caster who i will always pray to live long to help his children in the time of trouble, if you are here and you need your Ex back or your husband moved to another woman, do not cry anymore, contact this powerful spell caster now. Here’s his contact: Email him at: wiccalovespelltools@gmail.com or wiccalovespelltools@yahoo.com, you can also call him or add him on WhatsApp: +19085174108 check out his website : drjumbaspellhome.wordpress.com
ReplyDeleteAfter being in relationship with Wilson for seven years,he broke up with me, I did everything possible to bring him back but all was in vain, I wanted him back so much because of the love I have for him, I begged him with everything, I made promises but he refused. I explained my problem to someone online and she suggested that I should contact a spell caster that could help me cast a spell to bring him back but I am the type that don’t believed in spell, I had no choice than to try it, I meant a spell caster called Dr Zuma zuk and I email him, and he told me there was no problem that everything will be okay before three days, that my ex will return to me before three days, he cast the spell and surprisingly in the second day, it was around 4pm. My ex called me, I was so surprised, I answered the call and all he said was that he was so sorry for everything that happened, that he wanted me to return to him, that he loves me so much. I was so happy and went to him, that was how we started living together happily again. Since then, I have made promise that anybody I know that have a relationship problem, I would be of help to such person by referring him or her to the only real and powerful spell caster who helped me with my own problem and who is different from all the fake ones out there. Anybody could need the help of the spell caster, his email: spiritualherbalisthealing@gmail.com or WhatsApp him +15068001647 blog: spiritualherbal.blogspot.com you can email him if you need his assistance in your relationship or anything. CONTACT HIM NOW FOR SOLUTION TO ALL YOUR PROBLEMS
ReplyDeleteI'm so very grateful to Dr oluba, for bringing my husband who left me for another woman, that moment my husband Left me I thought I lost everything until a friend of my gave me Dr oluba, WhatsApp contact, I messaged him and told him the pain I was going through so he told me that everything was going to be fine that if I have the faith and believe in him that the spell will surely work for me and my husband will surely come back home and he told me what to do, so those things were done and 24 hrs later my husband came back home begging for my forgiveness, am so happy and grateful to Dr oluba, if you need his help email him at olubasolutiontemple@gmail.com or WhatsApp him +1(518)293-0860...
ReplyDeleteI'm Melvin Martins. from the United States. I started playing lottery games 7 years ago and I have never won big. I went online to seek help on how I can win big in my lottery games and I saw some nice reviews about Dr John who has made different people huge winners in their lottery games with his prayers. I gave it a try and I contacted Dr John who told me how and what to do before I can become a big lotto winner and I accepted. He made special prayers for me at his temple and gave me the required numbers to play the lotto game and when I played it, I won a massive $235.4 million POWERBALL jackpot. I was so happy and I chose to receive my winnings in a one-time, lump-sum payment of $160,038,447.27 at Florida Lottery Headquarters in Tallahassee and the balance was given to the store that I bought & played the game from. I would love other people to seek help from him and he can be contacted through WhatsApp/call/text via this number +1 (803) 820 2671 or you can email him on: Drjohnwinlotterspell@gmail.com .
ReplyDeleteIt was also hard for me to believe in any of the reviews and testimonies Online but after my Experience with this Dr, I feel it is only Right & Also a way to show my gratitude by sharing my own testimony too. I want to thank you Dr Ojogun for helping me to get my lovely partner back in my life. I am so blessed that you used a non forceful way of uniting and reuniting us. Our past, presence and future seems to have all merged into one. You told me that everyone has a compatible soul mate either in their life or waiting to come into their lives. I am glad its my partner of old. I did not want to really go and be with someone else.. You have removed the extra blockages that has been affecting us and holding us back. You can also reach out for HELP ON email; drojogunspellcaster@gmail.com Or Call WhatsApp +2348161638124 Page Link is https://www.facebook.com/pg/drojogunspell/
ReplyDeleteThanks for Sharing This Information. Project Management Assignment Help is a website that allows you to Assignment Help. Easily to write, Assignment Help Online with trained teachers ..
ReplyDeleteThis comment has been removed by the author.
DeleteThanks for sharing such a informatic blog If you are searching for best Python assignment help in Australia then Online Assignment Expert is the best choice for you. We provide top quality writing services at affordable rates. We have expert writers who provide you top rated services.
ReplyDeletePython assignment help
very useful information for the students who are interested in programming language. Do you know why python is the first choice for machine learning.. To read more- click@What Is The Reason Python Is The First Choice For Machine Learning? All about it
ReplyDeleteI was able to win back my Ex lover with the help of…. Dr.mac@yahoo.com..
ReplyDeleteMy name is Motina Nojus, I live and work here in the UK. My life is back!!! After 2 years of Broken marriage, my husband left me with two kids and my life was shattered. I felt like ending it all, I almost committed suicide because he left us with nothing. I was emotionally down all this while and life seem meaningless. On one faithful day as I was browsing through the internet, I came across several testimonies about Dr Ilekhojie. Some people testified that he brought their Ex coming back, some testified that he restores womb and heal sicknesses with herbs. I was more interested in reconiliation with my husband which Dr Ilekhojie made possible within 3 days. Now my husband is back and we are living happily since then. Thank you. Here, I leave his contact for anyone who needs help Email: gethelp05@gmail.com Whatsapp/Viber: +2348147400259
ReplyDeleteGrademiner's assistance with Python assignments is revolutionary! They stand out for their timely delivery, meticulous attention to detail, and reasonable prices. I wouldn't think twice about using their services once more.Python Assignment Help
ReplyDeleteThanks for sharing this insightful piece on Python coding for kids! It's great to see resources aimed at introducing programming concepts to young minds in a fun and engaging way. Keep up the fantastic work!
ReplyDelete