Python OOP Tutorial 1: Classes and Instances

///Python OOP Tutorial 1: Classes and Instances

Python OOP Tutorial 1: Classes and Instances

FavoriteLoadingAdd to favorites

In this Python Object-Oriented Tutorial, we will begin our series by learning how to create and use classes within Python. Classes allow us to logically group our data and functions in a way that is easy to reuse and also easy to build upon if need be. Let’s get started.

Python OOP 1 – Classes and Instances – https://youtu.be/ZDa-Z5JzLYM
Python OOP 2 – Class Variables – https://youtu.be/BJ-VvGyQxho
Python OOP 3 – Classmethods and Staticmethods – https://youtu.be/rq8cL2XMM5M
Python OOP 4 – Inheritance – https://youtu.be/RSl87lqOXDE
Python OOP 5 – Special (Magic/Dunder) Methods – https://youtu.be/3ohzBxoFHAY
Python OOP 6 – Property Decorators – https://youtu.be/jCzT9XFZ5bw

The code from this video can be found at:
https://github.com/CoreyMSchafer/code_snippets/tree/master/Object-Oriented

If you enjoy these videos and would like to support my channel, I would greatly appreciate any assistance through my Patreon account:
https://www.patreon.com/coreyms

Or a one-time contribution through PayPal:
https://goo.gl/649HFY

If you would like to see additional ways in which you can support the channel, you can check out my support page:

You can find me on:
My website – http://coreyms.com/
Facebook – https://www.facebook.com/CoreyMSchafer
Twitter – https://twitter.com/CoreyMSchafer
Google Plus – https://plus.google.com/+CoreySchafer44/posts
Tumblr – https://www.tumblr.com/blog/mycms

By |2017-05-01T12:23:17+00:00April 26th, 2017|Programming Video Tutorials, Python Video Tutorials|50 Comments

50 Comments

  1. June Seif April 26, 2017 at 9:08 pm - Reply

    I have gone through so many tutorials of Python trying to understand what the self parameter when initializing a function means and none of them has actually been able to break it down this way. Thank you so much and great explanation Correy.

  2. M.R. Bran April 26, 2017 at 9:14 pm - Reply

    Why would you do a whole other method to define the fullname when you can just create another instance variable? Thank you very much for the videos Corey. I really appreciate it.

  3. Tom Stockman April 26, 2017 at 9:18 pm - Reply

    This is a really great tutorial. Thanks!

  4. Hyper Gamer April 26, 2017 at 9:20 pm - Reply

    what ide You use

  5. halli manjunatha April 26, 2017 at 9:20 pm - Reply

    Thanks a lot , really helping ….

  6. John Richardson II April 26, 2017 at 9:21 pm - Reply

    Hi Mr. Schafer. I want to install anaconda; however, I already have python 3.6 installed on my computer. Do I need to uninstall python 3.6 before installing anaconda? Thank you very much.

  7. Alaweey98 April 26, 2017 at 9:22 pm - Reply

    Hi Corey, I sat and watched this 3 times and the more I watched it the clearer it became. Now I have my own code and have been stuck with something for days now.

    I have created a class called request as you can see below… however I have no idea how to get user input, I need to hold the request Id, password, name , programme, year of study, campus location, module name and a list of available days (Monday …Sunday) and timeslots (morning, afternoon, evening). The user must be able to enter as many unique timeslots as they wish on a single request. Can you please help me with this? How do I continue from here?

    class Request:

    def __init__(self, request_id, password, name, programme, year_of_study, campus_location, module_name, days):
    # This is creating methods within a class, and defining what arguments we want to accept

    self.request_id = request_id # Here I am setting the variables for the instances
    self.password = password
    self.name = name
    self.programme = programme
    self.year_of_study = year_of_study
    self.campus_location = campus_location
    self.module_name = module_name
    self.days = days

  8. Erik Knebel April 26, 2017 at 9:22 pm - Reply

    Very clear and helpful. Thank you!

  9. 罗弘熙 April 26, 2017 at 9:23 pm - Reply

    1000000 times better than my instructor! Thank you so much!

  10. Zaigham abbas April 26, 2017 at 9:24 pm - Reply

    which ide are u using?

  11. Amar Chhabra April 26, 2017 at 9:25 pm - Reply

    Thanks for the great video. Really helped me gain a fundamental understanding of classes. Thanks Again!

  12. Aria Farmani April 26, 2017 at 9:25 pm - Reply

    Thanks Corey, you gave a way better explanation than my teacher haha!

  13. Josh Lipovetsky April 26, 2017 at 9:30 pm - Reply

    Really well explained, Corey! Thank you for making this.

  14. Diego Alberto Rodríguez Mora April 26, 2017 at 9:32 pm - Reply

    Thanks so much Corey!

  15. Mihir Sevak April 26, 2017 at 9:32 pm - Reply

    In C++ one can overload constructors how can you do that in Python ? Also in C++ there are different access protections like public, private and protected. On top there are derived classes and friend classes. How can you apply those things in Python ?

  16. Denis Filippov April 26, 2017 at 9:32 pm - Reply

    Great explanation! Thanks

  17. flountHD April 26, 2017 at 9:33 pm - Reply

    Dude your awesome 🙂

  18. Siwei C April 26, 2017 at 9:33 pm - Reply

    Very clear video! Thanks!

  19. Fr0st P1xel April 26, 2017 at 9:33 pm - Reply

    which text editor are you using for these tutorials?

  20. Nicko Lawton April 26, 2017 at 9:34 pm - Reply

    I studied OOP for hours and never understood it. Watched 10 minutes of this video and everything made sense. Amazing.

  21. Hamid Abdanan April 26, 2017 at 9:34 pm - Reply

    After watching many of your videos, I realized it will be forever before I reach your level of knowledge. Is it possible to find a job before learning all you know and do most Python programmers know as much as you do? Or close to it?

  22. Hamid Abdanan April 26, 2017 at 9:37 pm - Reply

    I would like to repeat what robus has written earlier. I am a beginner (did I mention beginner) and would benefit from some sort of numbering of your videos. I am loving these videos.

  23. mohammed sawsaq April 26, 2017 at 9:37 pm - Reply

    thankssssssssssssssssss

  24. Harshit Kesarwani April 26, 2017 at 9:38 pm - Reply

    thank you very much for clearing my concepts on oop..this video couldn’t have been better..good job

  25. Mursaleen Nizamani April 26, 2017 at 9:40 pm - Reply

    Instead of making a new function called fullname couldn’t you just add
    self.fullname = first + ‘ ‘ + last
    under def__init__?

  26. Ahmad Ali April 26, 2017 at 9:41 pm - Reply

    You are a great teacher .Thank you

  27. Trevor Edwards April 26, 2017 at 9:43 pm - Reply

    Another great tutorial, Corey. Thank you so much for taking the time to do these.

  28. Okwukwe Ewurum April 26, 2017 at 9:44 pm - Reply

    Nice work.

  29. Corey Kirkwood April 26, 2017 at 9:44 pm - Reply

    what does ‘receive the instance as the first argument automatically’ mean? I am not sure why self is in front of everything in the ‘class Employee’

  30. Jenni Lithgow April 26, 2017 at 9:45 pm - Reply

    This is literally the best tutorial on Python classes that I have ever seen. I have been so stuck, trying to wrap my head around this, for an online class I’m taking at college, and have watched multiple Udemy classes and YouTube videos and it wasn’t until I watched this video that I understood it. Thank you so much for explaining this so clearly!!!

  31. Ping Chuan Tai April 26, 2017 at 9:45 pm - Reply

    omg you saved me

  32. Jasper van Leuven April 26, 2017 at 9:45 pm - Reply

    You didn’t even stutter

  33. Risheek Raju April 26, 2017 at 9:47 pm - Reply

    when i am typing this

    import sys
    import random
    import os

    class employee:
    def __init__(self,first,last,pay):
    self.first=first
    self.last=last
    self.pay=pay
    self.email=first+’.’+last+’@company.name’

    def fullname(self):
    return "{} {}".format(self.first,self.last)

    emp1=employee(‘risheek’,’raju’,70000)
    print(employee.fullname(emp1))
    print(emp1.fullname())

    i am getting this error:Traceback (most recent call last):
    File "C:Python27pythonpractice.py", line 16, in <module>
    print(employee.fullname(emp1))
    AttributeError: class employee has no attribute ‘fullname’

  34. Mauro Ymgch April 26, 2017 at 9:47 pm - Reply

    Thank you. Can’t imagine better explanation on a very confusing subject. It was great!

  35. Henrik Eckermann April 26, 2017 at 9:50 pm - Reply

    +Corey Schafer,

    I recalled your voice when watching this video, because I saw several of your tutorials in the past. I really like your videos. You can explain it very well and it makes a lot of fun. Thanks a lot for your effort. I subscribed to your channel.

  36. Nguimatsa Tatiana Flora April 26, 2017 at 9:51 pm - Reply

    I Love you Corey Schafer! 2 months not understanding and hating python! and just on a click I start understanding this language, liking it and saving my objectives!! Just WOW!!

  37. Maryam Ahmadi April 26, 2017 at 9:51 pm - Reply

    AMAZING
    Thanks a lot

  38. Mr Bad Guy April 26, 2017 at 9:51 pm - Reply

    Thanks bro. This is sooo helpful. Subscribed

  39. Algerchen Kordom April 26, 2017 at 9:52 pm - Reply

    You explain this very well. You are like an automated text book. And that is what some people want if they need to study during weekends. Thumbs up!

  40. learning anything April 26, 2017 at 9:55 pm - Reply

    in which software you doing this program

  41. BABLU KUMAR April 26, 2017 at 9:57 pm - Reply

    Thank you very much for the tutorial, +Corey Schafer .
    May I suggest a next topic? Well, you may like to teach about regex expressions.

  42. VelezBiH April 26, 2017 at 9:59 pm - Reply

    Awesome video, so i hear word "objects" used ex. Object Oriented Programming. Is "object" term mean same as "instance" of a class?

  43. Tom Stockman April 26, 2017 at 9:59 pm - Reply

    Is there an easy way to use an index with instances of a class? Like call emp_i.first?

  44. Amitesh Sahay April 26, 2017 at 10:02 pm - Reply

    Do you have videos on other pillar of OOPS(Abstraction, Polymorphism,Encapsulation)? I tried finding it, but I couldn’t..

  45. Belita d'souza April 26, 2017 at 10:03 pm - Reply

    What do you mean @13:35, "so we have to pass in the instance and that gets passed in as self". The instance is emp_1, so why does it become self?

  46. Newson Guershwin Monuma April 26, 2017 at 10:03 pm - Reply

    What IDE which you use?

  47. AMARNATH BUNGA April 26, 2017 at 10:03 pm - Reply

    great video, i loved the way you explained the things in step wise manner. Now i will start following your videos

  48. Chetan Soppimath April 26, 2017 at 10:03 pm - Reply

    crystal clear explanation , thanks a lot ……

  49. Miguel Nuno April 26, 2017 at 10:05 pm - Reply

    You are the best teacher… Peace out from German…

  50. Madan Sirish April 26, 2017 at 10:08 pm - Reply

    great explanation

Leave A Comment

*