Attributes in Python

Attributes in Python

Introduction

Any OOPs language has behaviour and characteristics properties. In Python, attributes are the same as characteristic properties in any other language. With the methods dir() attributes of python can be accessed. An example is demonstrated below:

Code

class ab:
    a = "class attribute"
    def __init__(self,b):
        self.b = b
print("Output: ")
obj = ab("instance attribute")
print(ab.a)
print(obj.b)

Output

Output:

class attribute
instance attribute

write your code here: Coding Playground

Types of attributes

Attributes can be of two types:

  • Class attributes
  • Instance attributes

Class attributes

In python, attributes that are defined out of the method but inside the python class object are known as class attributes. They belong to the class and can be accessed by class and instance objects.

Syntax

class name:
<class attributes>
<class attributes

Code

class ab:
    a = 10
print("Output:")
obj = ab()
print(obj.a)

Output

Output:

10

write your code here: Coding Playground

Instance attributes

The attributes that are defined inside the init method are called instance attributes. Using an instance of the class, we can access these attributes. They belong to an instance of objects.

Syntax

class name:
def __init__(self, ..)
        <instance attributes>
        <instance attributes

Code

class ab:
    a = "class attribute"
    def __init__(self,b):
        self.b = b  
print("Output:")
obj = ab("instance attribute")
print(obj.b)

Output

Output:

instance attribute

write your code here: Coding Playground

Uses of class attributes

For many different, we can use Class attributes. Some of them are mentioned below:

For creating a constant value variable
Across all the instances listing the objects
to provide a default variable

Across all the instances of the class, class attributes are initialized. So we can create use class attributes to create constant value variables in the class and these variables can be used in the functions. The class attribute also takes less time to initialize and has low memory space.

Code

class Area:
    pi = 3.14
    def __init__(self,rd):
        self.rd = rd
    def A(self):
        print("Area=", 2 * self.pi * self.rd)
       
    print("Output: ")
obj = Area(20)
print(obj.A())

Output

Output:

Area= 125.60000000000001
None

Explanation

Now in the above code, we created a variable named “pi” and it stores a constant value. Here, pi is a class attribute.

  • We created a method that will calculate the area.
  • And then we created an object of the class.
  • And then at last we print the area of the circle.

In a case where we want to access some properties of all instances of the class. When we access properties by calling each object it consumes very much time. By using class attributes, we can arrange all the instances of the class inside a single variable.

Namespaces in attributes

  • In python, a namespace is a dictionary, that allows us to store the key as an object and the value as an attribute. Now for this case, it can be divided into two parts:
  1. Object Namespace
  2. Class Namespace
  • Whenever a class instance tries to access any of the attributes, it will first search in the object namespace and then afterwards in the class namespace. The instance attribute is more before class attributes. It will be time-consuming when we access class attributes by the instance of the class.
  • Whenever a class try to access any of its attributes, it first searches in the class attribute and if otherwise throws an error.