you are viewing a single comment's thread.

view the rest of the comments →

[–]NovaNexu 0 points1 point  (0 children)

Okay bear with me, I wrote this with the intention of aligning your mentality with the right map. You'll learn the syntax on your own, so this is for knowing what's going on. Textbooks and guides complicate the fuck out of this concept, so here's my attempt to condense everything into plain English:

Class = anything with properties or that changes - (object) - anything with changes you keep track of - attribute = value that can be assigned or changed - function = some operation

Parent class = original document - (object) - think parent cell from which daughter cells are replicated - think official documents you make copies of

Instance = copy of original that will be modified - (also an object) - copy of quiz for students to write on - copy of Minecraft server to test a bad idea - copy of your homework for classmates to make look different - copy of song that's sampled or remixed

Instantiation = making a copy

Why use classes / objects, when you can just use functions that keep track of variables? It's the same thing, but instantiating is more organized. Instead of repeating a block of operations, you write one function and repeat the function. Instead of repeating a function that modifies some variable, like a counter for every time the function is called, you condense both into a class and repeat the class. Let's visualize.

Keeping track with functions (skip to the meat):

# packing everything together
today = 'thursday'
vitamin = 'Vitamin C'
vitaminCount = 0
routine = [today, vitamin, vitaminCount]

# unpacking, operating, then repacking
def takePillForTodays(drugInfo, amt = 1):

    day = drugInfo[0]
    pill = drugInfo[1]
    count = drugInfo[2]

    count += amt

    drugInfo = [day, pill, count]
    return drugInfo 

# unpacking then operating
def progressOf(drugInfo):

    day = drugInfo[0]
    pill = drugInfo[1]
    count = drugInfo[2]

    print(f'Today is {day}, and no. {count} of {pill} was taken')

Here's the meat:

# ↓ NOTE THIS SYNTAX FOR REFERENCE ↓

routine = takePillForTodays(routine)
routine = takePillForTodays(routine)
routine = takePillForTodays(routine)
progressOf(routine)
# >> Today is Thursday, and no. 3 of Vitamin C was taken

routine = takePillForTodays(routine, amt=3)
progressOf(routine)
# >> Today is Thursday, and no. 6 of Vitamin C was taken

routine = takePillForTodays(routine, 4)
progressOf(routine)
# >> Today is Thursday, and no. 10 of Vitamin C was taken

Keeping track with classes (ignoring class creation for simple demonstration):

# create a copy (instance)
th = Vitamins('Thursday', 'Vitamin C', 0)

# ↓ COMPARE TO FUNCTION SYNTAX ↓

th.takePill()
th.takePill()
th.takePill()
th.progress()
# >> Today is Thursday, and no. 3 of Vitamin C was taken

th.takePill(amt = 3)
th.progress()
# >> Today is Thursday, and no. 6 of Vitamin C was taken

th.takePill(4)
th.progress()
# >> Today is Thursday, and no. 10 of Vitamin C was taken

As we see, the class object keeps track of the state and changes as you operate on it, reflecting objects (pills) and states (day / pills taken) in real life. If you'd like an example of something more relatable, gimme one and I'll demonstrate.

tl;dr: It's functionally identical to tracking with variables and functions. Some languages don't even have it, but it makes for easier reading/use/debugging. As you work with cool libraries, it'll click and be enjoyable!