Class and instance attributes

Mouna Ben Ali
4 min readJan 26, 2022

--

Object Oriented Programming is not a programming language, but rather a model in which programs are organized around data or objects. OOP uses the concept of objects and classes. A class can be thought of as a ‘blueprint’ for objects. These can have their own attributes (characteristics), and methods (actions they perform). In Python everything is an object. Objects are a representation of real world objects like cars, dogs, house, etc. They all share data and behavior.

As an object oriented language, Python provides two scopes for attributes: class attributes and instance attributes. Think of a Class like a blueprint from which different objects are created with the same blueprint.

Each object, or in this case a car, is an instance of class car (image below). The cars can have data like number of wheels, doors, seating capacity, and behaviors: accelerate, stop, display how much fuel is left and more. Data in a class are called attributes and behaviors are called methods.

Data → Attributes & Behavior → Methods

Again, a class is like a blueprint for which other objects can be created. So with class instrument, objects like piano and guitar can be made. From guitar other objects can be made from it and they also can inherit attributes and methods from guitar and instrument if applied to them.

What’s a class attribute:

A class is the description of data called attributes, and of operations called methods. A class is a definition model for objects with the same set of attributes, and the same set of operations. From a class you can create one or more objects by instantiation; each object is an instance of a single class. The characteristics of object programming are: Encapsulation of data (attributes) and behaviors (methods): attributes and methods are defined in the same environment (capsule). Hiding information: the class user may not have direct access to the attributes. Inheritance: you can define a class from another class. Polymorphism: the same method can behave differently depending on the instance to which it is applied.

What’s an instance attribute:

In object-oriented programming, an instance variable is a variable containing the state of an object, also called an attribute. An instance variable specifies the state of an object to which it refers. Two different objects, even belonging to the same class, can have different values ​​in their respective instance variables. From outside the object, instance variables cannot be altered or even viewed (that is, instance variables are never public), except by methods explicitly provided by the programmer. Like global variables, instance variables are set to “null” until they are initialized. Typically instance variables do not have to be declared. This gives an extremely flexible object structure. In fact, each instance variable is dynamically added to the object when it is first invoked. It is opposed to the class variable, also called static variable.

Differences Between Class and Instance Attributes

The difference is that class attributes is shared by all instances. When you change the value of a class attribute, it will affect all instances that share the same exact value. The attribute of an instance on the other hand is unique to that instance.

What are all the ways to create them and what is the Creating Classes and Instance Pythonic and non-Pythonic

A non-Pythonic way would be like this:

class Square:
def __init__(self, size=0):
if not isinstance(size, int):
raise TypeError("size must be an integer")

if size < 0:
raise ValueError("size must be >= 0")

self.__size = size * size def area(self):
return self.__sizet__width()

The disadvantage comes when we need to modify the code. The pythonic way of doing it would be to use getter and setter property methods.

class Square:
def __init__(self, size=0):
self.size = size @property
def size(self):
return self.__size @size.setter
def size(self, value):
if not isinstance(value, int):
raise TypeError("size must be an integer")
if value < 0:
raise ValueError("size must be >= 0")
self.__size = value def area(self):
return self.__size * self.__size

The advantage of using property allows us to attach code to the self.size attribute and any code assigned the value of size will be called with size in def size.

Python is not my favorite introduction to object oriented programming, or OOP, but it’s definitely not the worst. Just remember, everything in Python is an object.

Object Methods

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

What are the advantages and drawbacks of each of them:

if you need to modify the code. The pythonic way of doing it would be to use getter and setter property methods

class obj:

def __init__(self, age = 0):

self._age = age

# getter method

def get_age(self):

return self._age

# setter method

def set_age(self, x):

self._age = x

How does Python deal with the object and class attributes using the __dict__

A __dict__ is a dictionary or maping objet used to store an object’s attributes. How does Python deal with the object and class attributes using the __dict__ ? Well, each instance is stored in a dictonary.

--

--