Python class: Objects and classes


Technology always evolves. What are classes and where do they come from?

1. Statements:
In the very early days of computing, programmers wrote only commands.

2. Functions:
Reusable group of statements, helped to structure that code and it improved readability.

3. Classes:
Classes are used to create objects which have functions and variables. Strings are examples of objects: A string book has the functions book.replace() and book.lowercase(). This style is often called object oriented programming.

Lets take a dive!

Python Courses:

Python class

We can create virtual objects in Python. A virtual object can contain variables and methods.  A program may have many different types and are created from a class. Example:

class User:
    name = ""
    def __init__(self, name): = name
    def sayHello(self):
        print "Hello, my name is " +
# create virtual objects
james = User("James")
david = User("David")
eric = User("Eric")
# call methods owned by virtual objects

Run this program. In this code we have 3 virtual objects: james, david and eric.  Each object is instance of the User class.

python class: creation of objects
Python Class: create objects

In this class we defined the sayHello() method, which is why we can call it for each of the objects.  The __init__() method is called the constructor and is always called when creating an object.  The variables owned by the class is in this case “name”. These variables are sometimes called class attributes.

We can create methods in classes which update the internal variables of the object. This may sound vague but I will demonstrate with an example.

Class variables

We define a class CoffeeMachine of which the virtual objects hold the amount of beans and amount of water. Both are defined as a number (integer). We may then define methods that add or remove beans.

    def addBean(self):
        self.bean = self.bean + 1
    def removeBean(self):
        self.bean = self.bean - 1

We do the same for the variable water. As shown below:

class CoffeeMachine:
    name = ""
    beans = 0
    water = 0
    def __init__(self, name, beans, water): = name
        self.beans = beans
        self.water = water
    def addBean(self):
        self.beans = self.beans + 1
    def removeBean(self):
        self.beans = self.beans - 1
    def addWater(self):
        self.water = self.water + 1
    def removeWater(self):
        self.water = self.water - 1
    def printState(self):
        print "Name  = " +
        print "Beans = " + str(self.beans)
        print "Water = " + str(self.water)
pythonBean = CoffeeMachine("Python Bean", 83, 20)
print ""

Run this program. The top of the code defines the class as we described.  The code below is where we create virtual objects. In this example we have exactly one object called “pythonBean”.  We then call methods which change the internal variables, this is possible because we defined those methods inside the class.  Output:

Name  = Python Bean
Beans = 83
Water = 20
Name  = Python Bean
Beans = 84
Water = 20


60 thoughts on “Python class: Objects and classes

  1. Frank
    Miguel - March 1, 2016


    I don’t understand yet the word “self”. In the following example, the code didn’t work until I put the word “self” on the argument of the function “Imprimir”

    class myAccount:
        name = ""
        SavedMoney = 0
        bills = 0
        deposits = 0
        def __init__(self, name, midinero, bills, deposits):
   = name
            self.SavedMoney = midinero
            self.bills = bills
            self.deposits = deposits
        def addMoney (self, amount):
            self.SavedMoney = self.SavedMoney + amount
        def Imprimir(self):
    micuenta = myAccount("Miguel", 200, 0, 0)

    why do I have to take “self” as argument in every function?

    1. Frank
      Frank - March 2, 2016

      Without using the self keyword you are using local variables. Local variables cannot be accessed outside the functions, because of the scope of the variable.

      For illustration, consider this code:

          def __init__(self, name, midinero, bills, deposits):
     = name
              self.SavedMoney = midinero
              self.bills = bills
              self.deposits = deposits
              onlyInScope = 3

      The variable onlyInScope can only be accessed within __init__(). Adding the self keyword makes them accessibly outside by newly created objects. Adding self will tell Python these are class variables.