Python class and instance explained in simple terms

I was trying to explain this to someone in the forum, and decided it's time to re-write how class's, instances, object, and self work. 

Here it goes, a big ol' metaphor.

A Python class is like a blueprint.

I have all the technical data inside it to lay out how to build a particular object.
The Python compiler is going to store that code away, and then you can 'access' it, or build something from that blueprint.  The thing that is built is the instance.

class Spamalot(object):
    def __init__(self):
        self.spam = "spam"
    def give_me_spam(self):
        print("here is your ", self.spam)

It's just a silly class that doesn't do much.  Python has much more complex class's stored away in it's library for us to use on a regular basis.  Blueprints to something like : BaseException or modules like math.

We create instances of Spamalot because we don't want to ruin the original blueprint.   Perhaps I want to change  Spamalot's menu.  I want bacon instead of spam.

I can create an instance:

x = Spamalot()

and now I can change that active copy ((instance)) of Spamalot without altering the original blueprint.  

x.spam = "bacon"

As for self, it can be any word.  It's tradition to use self, but basically by doing the 'self.spam = "spam"',    instead of 'spam = "spam"' , the first version belongs to the original blueprint,  and once an instance is created, that self will belong specifically to that blueprint.  That second version has no loyalty to the class and will abandon it's post whenever it's told. 

class Sillyness(object):
  anything_exterior = "change at a whim"
  def __init__(belongsToSillyness):  <= or self
    belongsToSillyness.anything = "green eggs and ham"
  def show_me_silly(self):
    print("what is something silly in Sillyness?", belongsToSillyness.anything)
  def show_me_disloyalty(self):
    print("did the rat abandon ship yet?", anything_exterior)

Because 'anything_exterior' is not labeled, identified to the compiler by that 'self',  If it is altered by any instance, or any exterior function, it will alter the blueprint value.   Now that person that did not want pickles on their sandwich is getting a pickle because someone changed the menu, the recipe,  the blueprint and everyone get's a pickle.


If this were a much more complex blueprint... Say a super car with specialty tires I would like to modify,  I could build from my blueprint the super car, and change the specialty tires or the paint, or the cd player, without changing the original design of that car. 

The class is an object, the blueprint for that object.
The instance is what the compiler built from that blueprint, also an object. But the instance can be modified, used, and deliver delicious spam whenever you feel the hankering for a salty pork like meat cube.

As long as it's on the menu. 

Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame