Python

Python Menu

Objects are an encapsulation of variables and functions into a single entity. Objects get their variables and functions from classes. Classes are essentially a template to create objects.

A very basic class would look something like:

class MyClass:
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

An explanation on why you have to include that self as a parameter will be given later. First, to assign the above class(template) to an object you would do the following:

class MyClass:
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

myobjectx = MyClass()

Now the variable myobjectx holds an object of the class MyClass that contains the variable and the function defined within the class called MyClass.

Accessing Object Variables

To access the variable inside the newly created object myobjectx you would do the following:

class MyClass:
    myString = "Hello"

    def function(self):
        print("This is a message inside the class.")

myobjectx = MyClass()
myobjectx.myString # accessing the variable 'myString'

So for instance the below would output the string "Hello":

class MyClass: myString = "Hello"
def function(self): print("This is a message inside the class.")
myobjectx = MyClass() myobjectx.myString #accessing the variable 'myString' print(myobjectx.myString)

You can create multiple different objects that are of the same class(have the same variables and functions defined). However, each object contains independent copies of the variables defined in the class. For instance, if we were to define another object with the "MyClass" class and then change the string in the variable above:

class MyClass: myString = "Hello"
def function(self): print("This is a message inside the class.")
myobjectx = MyClass() myobjecty = MyClass()
myobjecty.myString = "Python"
# Then print out both values print(myobjectx.myString) print(myobjecty.myString)

Accessing Object Functions

To access a function inside an object you use notation similar to accessing a variable:

class MyClass: variable = "blah"
def myFunction(self): print("This is a message inside the class.")
myobjectx = MyClass()
myobjectx.myFunction()

Exercise

We have a class defined for vehicles. Create two new vehicles called car1 and car2. Set car1 to be a red convertible worth 60,000.00 with a name of Ferrari, and car2 to be a blue van named GMC worth 10,000.00.

# define the Vehicle class class Vehicle: name = "" type= "car" color = "" value = 100.00 def description(self): desc_str = "%s is a %s %s worth %.2f." % (self.name, self.color, self.type, self.value) return desc_str
# your code goes here
# test code print(car1.description()) print(car2.description())
# define the Vehicle class class Vehicle: name = "" type= "car" color = "" value = 100.00 def description(self): desc_str = "%s is a %s %s worth %.2f." % (self.name, self.color, self.type, self.value) return desc_str
# your code goes here car1 = Vehicle() car1.name = "Ferrari" car1.color = "red" car1.type= "convertible" car1.value = 60000.00
car2 = Vehicle() car2.name = "GMC" car2.color = "blue" car2.type= "van" car2.value = 10000.00
# test code print(car1.description()) print(car2.description())
test_output_contains("Ferrari is a red convertible worth 60000.00.") test_output_contains("GMC is a blue van worth 10000.00.") success_msg("Excellent!")

Introduction

Python Basics

Python Advance

Data Science Python Tutorials

Python Functions and Methods