Class and instance attributes

Arthur Mayer
3 min readJan 11, 2021

--

Dog class

In this article we will see what are classes and instances in OOP (object oriented programming) with Python language.
Summary:
0. Definitions
1. Code examples
2. User manipulations

0. Definitions

Documentation is difficult to read but necessary to code. Here is an abstract of all concepts we will need.

Object-oriented programming (OOP) is a programming paradigm based on the concept of objects which contain data and code: data fields (classes & instances), and code procedures (methods, functions).

A Class define a set of properties and methods that are common to objects and the behavior and information our objects will contain.

An Object is a group of related variables and methods (functions).

An instance method is a function bounded to a class instance. It is opposed to a static method or a class method. It is a specific representation of an object. An object is a generic thing while an instance is a single object that has been created in memory. Usually an instance will have values assigned to it’s properties that differentiates it from other instances.

An instance attribute is a variable belonging to an object. It is defined inside the constructor function of a class which is called __init__(self, ...).

There are 3 types of instance attributes (or access modifiers) :
Public instance attributes can be accessed anywhere inside or outside the class.
_Protected instance attributes (called with single underscore prefix) can be accessed within the same package.
__Private instance attributes (called with a double underscore prefix) are used in order to avoid direct access of a class field and cannot be accessed directly or modified by external user. Moreover, we can use them to add validation logic (data type checks for example) around getting and setting a value. They will need to use getter and setter accessors to ensure data encapsulation.

A class attribute is a variable belonging to a class. Thus, it is shared between all other objects and is defined outside the constructor function.

Each object and attribute are stored in a dictonary special attribute which is called __dict__(example shown in section 2).

1. Code examples

Lets stop talking, there is nothing more clearer than a code block !

Create a class and a constructor:

>>> class Dog:
... """
... Create a "Dog" class
... """
... def __init__(self, name, weight, age, address):
... """
... Constructor
... Args:
... name (string): 'name' instance attribute
... weight (int): 'weight' instance attribute
... etc...
... """
#this is a private instance attribute:
... self.__name = name
#these are public instance attribute:
... self.weight = weight
... self.age = age
... self.address = address

Create an instance and instance attribute properties (getter & setter):

...    def Characteristics(self):
... """
... Create a "Characteristics" instance method
... """
...
print("{} weighs {}kg and is {} year(s) old."
.format(self.name, self.weight, self.age))
... @property
... def name(self):
... """
... Create the name getter
... """
... return self.__name
... @name.setter
... def name(self, value):
...
"""
... Create the name setter
... """
... if type(value) is not int:
... raise TypeError("Name must be a string !")
... else:
... self.__name = value

2. User manipulations:

Now, lets manipulate our new Labrador class

Creating a ‘Labrador’ class with proper attributes:

>>> Labrador = Dog(Poopie, 25, 9, garden, female)
>>> Labrador.Characteristics()
Poopie weighs 25kg and is 9 year(s) old.

Trying to change ‘name’ value and data type:

>>> Labrador.name = "Loolee"
>>> Labrador.name
Loolee
>>> Labrador.name = 98
>>> Labrador.name
TypeError: Name must be a string !

The dictionnary attribute:

>>> Labrador.__dict__
{'name': Poopie, 'weight': 25, 'age': 9, 'address': garden}

--

--