How to Create an incremental ID in a Class in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Create an incremental ID in a Class in Python
  2. Specifying a different count value to start from
  3. Creating an incremental ID in a Class manually

# Create an incremental ID in a Class in Python

To create an incremental ID in a class:

  1. Use the itertools.count() method to get an auto-incrementing count object.
  2. Assign the id instance variable to the result of calling next() with the count object.
  3. Each class instance will have a unique id attribute.
main.py
import itertools class Employee(): id_obj = itertools.count() def __init__(self, name, salary): self.id = next(Employee.id_obj) self.name = name self.salary = salary alice = Employee('Alice', 100) bob = Employee('Bobbyhadz', 100) carl = Employee('Carl', 100) print(alice.id) # ๐Ÿ‘‰๏ธ 0 print(bob.id) # ๐Ÿ‘‰๏ธ 1 print(carl.id) # ๐Ÿ‘‰๏ธ 2

create an incremental id in class

The code for this article is available on GitHub

We used the itertools.count() method to make an iterator that returns an auto-incrementing count object.

main.py
import itertools id_obj = itertools.count() print(next(id_obj)) # ๐Ÿ‘‰๏ธ 0 print(next(id_obj)) # ๐Ÿ‘‰๏ธ 1 print(next(id_obj)) # ๐Ÿ‘‰๏ธ 2

Notice that we assign the id attribute of each instance in the __init__() method to the result of calling next() with the count object.

The itertools.count() method takes a start argument that is set to 0 by default.

# Specifying a different count value to start from

You can specify a value for the start argument if you need to create an incremental ID that starts at 1 or any other value.

main.py
import itertools class Employee(): # ๐Ÿ‘‡๏ธ Start incremental ID at 1 id_obj = itertools.count(1) def __init__(self, name, salary): self.id = next(Employee.id_obj) self.name = name self.salary = salary alice = Employee('Alice', 100) bob = Employee('Bobbyhadz', 100) carl = Employee('Carl', 100) print(alice.id) # ๐Ÿ‘‰๏ธ 1 print(bob.id) # ๐Ÿ‘‰๏ธ 2 print(carl.id) # ๐Ÿ‘‰๏ธ 3

specify different count value to start from

The code for this article is available on GitHub

We specified a value of 1 for the start argument, so the auto-incrementing IDs start at 1.

Each time a class instance is created, the __init__() method runs and its id attribute gets set to the result of calling next() with the count object.

An iterator object represents a stream of data. Every time we pass the iterator to the next() function, the next item in the stream is returned.

# Creating an incremental ID in a Class manually

You can also manually manage the ID attribute by using a class variable.

main.py
class Employee(): cls_id = 0 def __init__(self, name, salary): self.id = Employee.cls_id self.name = name self.salary = salary Employee.cls_id += 1 alice = Employee('Alice', 100) bob = Employee('Bobbyhadz', 100) carl = Employee('Carl', 100) print(alice.id) # ๐Ÿ‘‰๏ธ 0 print(bob.id) # ๐Ÿ‘‰๏ธ 1 print(carl.id) # ๐Ÿ‘‰๏ธ 2
The code for this article is available on GitHub

The class has a cls_id class variable that is incremented by 1 each time an instance is created.

When an instance is created, the __init__() method runs, where we set the id attribute on the instance to the current cls_id value and increment the value of the cls_id class variable.

You can change the initial value of the cls_id attribute if you need to start counting from a different value.

main.py
class Employee(): cls_id = 1 def __init__(self, name, salary): self.id = Employee.cls_id self.name = name self.salary = salary Employee.cls_id += 1 alice = Employee('Alice', 100) bob = Employee('Bobbyhadz', 100) carl = Employee('Carl', 100) print(alice.id) # ๐Ÿ‘‰๏ธ 1 print(bob.id) # ๐Ÿ‘‰๏ธ 2 print(carl.id) # ๐Ÿ‘‰๏ธ 3
The code for this article is available on GitHub

The class has an incremental cls_id attribute starting at 1 instead of 0.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev