I myself have had (still have to a certain extend) a hard time wrapping my head around instancing and classes in general. Here is my basic understanding of it so far. I am going to try to explain my understanding of a class in as general real world use case as I can.
I look at a class as a template or recipe to create something. lets take a book for example. How would you describe a book? it need pages, it has an author, it has a release date, it has a genre etc. These descriptions of what defines a book is what we would put in our init() function. So a class would look something like this (I am going to be using python 2 syntax, as I personally feel it makes a little more sense):
def __init__(self, title, genre, author, release_date, amount_of_pages):
self.title = title
self.genre = genre
self.author = author
self.release_date = release_date
self.amount_of_pages = amount_of_pages
the above example can now be used a template to create a book (a book object). So lets say you had 5 empty books on a table, and you wanted to grab one of them and write your story inside, add you as a author, give it a title etc.
You grabbed book number four. This book is not unique in anyway yet, as its just an blank book looking exactly like the others. How can we make it unique? well we can give it a title, put on an author, add it to a genre and so on… and we can use our defined class for that as follows:
my_book = Book('understanding_classes','programming', 'Jesper Rahlff', '06-06-2020', 200)
notice that the class we defined at the start takes an object? that object is our blank book that we selected (book number four), and every time our code uses the self argument we refer to that specific book (book number four) that we selected.
Once that blank book have been through our template/recipe, its now unique and quite easy to identify on the table amongst the other books (that is still just blank) we did not select. We can even check the unique instance id that belongs to my_book by using
the outcome of all of this is that I have now created a unique book (an instance), which I am the author of, with my chosen title, in my favorite genre.
so why use a class for this stuff instead of just defining some variables like title, genre, author etc?
well in programming its very handy to be able to reuse code. so if I wanted to create 10 books I could now do that with one line of code per book:
my_first_book = Book('understanding_classes','programming', 'Jesper Rahlff', '06-06-2020', 200)
my_second_book = Book('did i really understand classes?','programming', 'Jesper Rahlff', '07-06-2020', 201)
my_third_book = Book('I think I get it','programming', 'Jesper Rahlff', '08-06-2020', 202)
all of these are now unique objects which all used the Book() class as a recipe (is an intance of) to get created.
I hope this helps anyone that is struggling with the concept. This is by no means a perfect understanding of the complete usage of Classes. I am just a fellow learner trying to navigate the world of python .self