Categories:

Classes and Objects in Python

Python, since day 1, has been an object-oriented language; therefore we should expect to utilize an object within our projects. An object obtains their variables and functions from the classes, which are a template for generating an object.

Example:

class class_value:
    variable = "This is a value for the class"

    def function(self):
        print("Here is our message we are putting inside the class.")

You may be wondering, what is that “self?” In object-oriented programming, whenever we define methods for a class, we use self as the first parameter in each case.

To assign a class to an object, you can do this:

Example:

class class_value:
    variable = "This is a value for the class"

    def function(self):
        print("Here is our message we are putting inside the class.")
x = class_value()

The variable “x” now has the object, of the class “class_value.”

How do we access it then?

To access an object variable, you can do:

Example:

class class_value:
    variable = "This is a value for the class"

    def function(self):
        print("Here is our message we are putting inside the class.")

x = class_value()
x.variable

Example:

class class_value:
    variable = "This is a value for the class"

    def function(self):
        print("Here is our message we are putting inside the class.")

x = class_value()
print(x.variable)

You can also make multiple objects that are different, out of the same class. Keep note that each object would contain independent copies of each variable defined per class.

Example:

class class_value:
    variable = "This is a value for the class"

    def function(self):
        print("Here is our message we are putting inside the class.")
x = class_value()
y = class_value()
z = class_value()

z.variable = "This is some text"

# print all values
print(x.variable)
print(y.variable)
print(z.variable)

How about accessing the object’s functions?

To access the function of the object, it is easy: just switch “variable” to “function.”

class class_value:
    variable = "This is a value for the class"

    # this would get printed
    def function(self):
        print("Here is our message we are putting inside the class.")

x = class_value()

x.function()

Exercise

Lets try an exercise to see if we understand classes and objects. For this sample, we are going to have a class that is defined for houses. We want to create two new houses called “house1” and “house2.” One of the houses, a white house, will be worth $500,000.00 and will be a condo. The second house, a grey house, will be worth $800,000.00 and will be a “ingle family. Please take a look at the solution if you get stuck.

Exercise:

# define the house class
class House:
    address = ""
    type = "house"
    color = ""
    value = 100.00
    def description(self):
        desc_str = "%s is a %s, that is colored %s, and is listed at $%.2f." % (self.address, self.color, self.type, self.value)
        return desc_str

# your code goes here

# test code
print(house1.description())
print(house2.description())

Solution:

# define the house class
class House:
    address = ""
    type = "house"
    color = ""
    value = 100.00
    def description(self):
        desc_str = "%s is a %s, that is colored %s, and is listed at $%.2f." % (self.address, self.color, self.type, self.value)
        return desc_str

# your code goes here
house1 = House()
house1.address = "1234 Sycamore"
house1.color = "white"
house1.type = "Condo"
house1.value = 500000.00

house2 = House()
house2.address = "4321 Eromacys"
house2.color = "grey"
house2.type = "Single Family"
house2.value = 800000.00

# test code
print(house1.description())
print(house2.description())

Output:

<script.py> output:
    1234 Sycamore is a Condo, that is colored white, and is listed at  $500000.00.
    4321 Eromacys is a Single Family, that is colored grey, and is listed at $800000.00.

For more information

If you seek to learn more about Python, please visit Python’s official documentation.