Instance attributes python – Python Class and Instance Attributes with Examples

Instance attributes python: Class attributes are unique to the class( belong to the class itself ) and will be shared by all instances. For legibility, such properties are defined in the class body portions, usually at the top.

Attributes are the main characters of a programming language. They are in charge of storing crucial data values and assisting with data manipulation.

Class Attribute in Python:

A Python Class Attribute is a variable or attribute that is contained within a Class. That is, its scope is restricted to the Python class.

The Class attribute only makes one copy of itself, which is shared and used by all of the functions and objects in that class.

Syntax:

class Class_name:
    variable = value

Example

Approach:

  • Create a class say Example_classattr.
  • Inside the class, take a variable and initialize its value with 1.
  • Create a function say mult.
  • Apply some Arithmetic operation for the above class variable and print it.
  • Create an object for the above class Example_classattr and store it in a variable as ‘fst_objct’.
  • Call the mult function using the above-created object.
  • Similarly, Create another object for the above class Example_classattr and store it in another variable as ‘scnd_objct’.
  • Call the mult function using the above-created object.
  • The Exit of the Program.

Below is the implementation:

# Create a class say Example_classattr.
class Example_classattr:
    # Inside the class, take a variable and initialize its value with 1.
    numb = 1
    # Create a function say mult.

    def mult(self):
        # Apply some Arithmetic operation for the above class variable and print it
        Example_classattr.numb *= 5
        print(Example_classattr.numb)


# Create an object for the above class Example_classattr and
# store it in a variable as 'fst_objct'
fst_objct = Example_classattr()
# Call the mult function using the above created object
fst_objct.mult()
# similarly, Create another object for the above class Example_classattr and
# store it in a variable as 'scnd_objct'
scnd_objct = Example_classattr()
# Call the mult function using the above created object
scnd_objct.mult()

Output:

5
25

Both of the objects produced us the same copy of the variable ‘numb,’ as can be seen. As a result, numb= 1 at first.

When the object ‘fst_objct’ calls the function, the same copy of ‘numb’ is used (the value is not reset), and so val=5 is returned. When called by the scnd_objct, ‘numb’ becomes numb*5, which is 5*5= 25.

Instance Attribute in Python:

In contrast to class attributes, instance attributes are not shared between objects. Every object has a duplicate of the instance attribute whereas in class attributes all objects refer to a single copy.

We have two functions for listing the attributes of an instance/object:

1. vars(): The vars() function displays the attribute of an instance as a dictionary.
2. dir(): Because it is not confined to instances, this function displays more attributes than the vars() function. It also shows the class attributes. It also has the features of its ancestor classes.

Syntax:

def function_name():
    variable = value

Example

Approach:

  • Create a class say Example_instanceattr.
  • Inside the class, Create a function say mult.
  • Inside the function take a variable and initialize its value with 1.
  • Apply some Arithmetic operation for the above function variable and print it
  • Create an object for the above class Example_instanceattr and store it in a variable as ‘fst_objct’.
  • Call the mult function using the above-created object.
  • Similarly, Create another object for the above class Example_instanceattr and store it in another variable as ‘scnd_objct’.
  • Call the mult function using the above-created object.
  • The Exit of the Program.

Below is the implementation:

# Create a class say Example_instanceattr.
class Example_instanceattr:
    # Inside the class, Create a function say mult.
    def mult(self):
        # Inside the function take a variable and initialize its value with 1.
        numb = 1
        # Apply some Arithmetic operation for the above function variable and print it
        numb *= 5
        print(numb)


# Create an object for the above class Example_instanceattr and
# store it in a variable as 'fst_objct'
fst_objct = Example_instanceattr()
# Call the mult function using the above created object
fst_objct.mult()
# similarly, Create another object for the above class Example_instanceattr and
# store it in a variable as 'scnd_objct'
scnd_objct = Example_instanceattr()
# Call the mult function using the above created object
scnd_objct.mult()

Output:

5
5

Here, we declared and set an instance attribute to numb=1.

Furthermore, when fst_objct attempts to access the variable via the function, it creates a new copy, resets the default value to the initialized value, and then grants access to it.

When scnd_objct tries to access the instance variable ‘numb,’ the identical problem occurs.