Classes and Objects

Objects are like a box of variables and functions all in a single entity. Objects get their variables and function from classes. Classes are basically a template in order to create objects.

A basic class would look like this:

class MyClass:

line = "Hello"

def function(self):

print("This is a message from within the class")

To assign the above class to an object you would need to have it as a variable.

myobject1 = MyClass

Now the variable myobject1 holds an object of the class "MyClass" which has a variable and a function within it.

Accessing Object Variables
To access the "line" variable within the class you would need to have to do the following:

myobject1.line

* Outputs the line "Hello"*

You can create multiple different objects within the same class if needed. Although, each objects is an independant copy of the class contents and so one object's contents are individual from the template's contents.

Accessing Object Functions
To access a function inside of an object you simply need to assign a variable to the object like so:

myobject1 = Class

Then you'll want to call the function by doing so:

myobject1.function

* Calls the function (.function is the name of the function, change it to whatever you have named your function within the object)*

Continue to the next lesson.