Python: Class and Object

By Xah Lee. Date: . Last updated: .

Define a class

A class is a boxed set of functions and variables. It defines a blueprint, of what variable and functions is together as a single thing.

Define a class like this:

# define a class
class Dog:
    "A class example"

    # a class variable
    legs = 4

    # define a method.
    # first parameter must must be “self”.
    def number_of_legs(self):
        return self.legs

    # example a method with 2 args for itself.
    def add(self, x, y):
        return x + y

Class name starts with a capital letter

by convention.

Class attributes

In python, variable and methods of a class are called attributes.

Instantiate a class, create a object

Class is just a blueprint of structure. Class is useless by itself, in the same way a function is useless if you don't call it.

You need to create a instance of the class to actually do something. Instance of a Class is called Object.

# example of a class, and create a instance of it

class Dog:
    "A class example"

    # a class variable
    legs = 4

    # define a method.
    # first parameter must must be “self”.
    def number_of_legs(self):
        return self.legs

    # example a method with 2 args for itself.
    def add(self, x, y):
        return x + y

# create a object of the class Dog.
# This is called instantiating a class.
xx = Dog()

# Data or functions defined in a class are called the class's attributes or methods.
# To access them, append a dot and their name after the object's name.
print(xx.legs)
# 4

# call a method
print(xx.number_of_legs())
# 4

# call a method
print(xx.add(1, 2))
# 3

Define constructor (__init__)

You can define a method in a class such that it'll be automatically called when the class is instantiated. Such a method is called constructor.

# A example of class with explicitly defined constructor.

class Cat:
    "A class example with explicitly defined constructor."

    # method named __init__ is special. It is automatically called whenever a class is instantiated.
    def __init__(self):
        print("constructor called")

    # by convention, __init__ is placed at top of method def in a class


xx = Cat()
# constructor called

Class variable and instance variable

In python, the class variables are shared among all instances, but each instance can define its own variable, e.g. object.x = 3, and if there is a class variable of the same name, it effectively shadows it.

# each instance share the class variable initially, but as soon as they set value, they have own copy

# define a class
class Bat:
    "A class example"

    legs = 2 # a class variable

big_bat = Bat()
small_bat = Bat()

# each instance share the class variable initially
print(big_bat.legs == 2) # True
print(small_bat.legs == 2) # True

# you can set the class variable for all instances
Bat.legs = 4
print(big_bat.legs == 4) # True
print(small_bat.legs == 4) # True

# instance variable can create its own arbitrary variables like this
big_bat.legs = 5
small_bat.legs = 6

print(big_bat.legs == 5) # True
print(small_bat.legs == 6) # True

# if object's variable clash with class variable, the object's own variable effectively overrides the class variable.
Bat.legs = 2
print(big_bat.legs == 5) # True
print(small_bat.legs == 6) # True

Instance variable

To have true instance variable, declare them inside constructor, with self. prefix on the variable.

# true instance variable example

class SpaceDog:
    "A class example"

    def __init__(self, x):
        # instance variable
        self.eyes = x

    def put(self, x):
        self.eyes = x

    def get(self):
        return self.eyes

# this has no effect
SpaceDog.eyes = 22222

aa = SpaceDog(7)
bb = SpaceDog(8)

print(aa.eyes == 7)  # True
print(aa.get() == 7)  # True

print(bb.eyes == 8)  # True
print(bb.get() == 8)  # True

aa.put(3)
print(aa.get() == 3)  # True

bb.put(4)
print(bb.get() == 4)  # True

Python. Class and Object