Last updated: Apr 9, 2024
Reading timeยท3 min

To create an incremental ID in a class:
itertools.count() method to get an auto-incrementing count
object.id instance variable to the result of calling next() with the
count object.id attribute.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

We used the
itertools.count()
method to make an iterator that returns an auto-incrementing count object.
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.
itertools.count() method takes a start argument that is set to 0 by default.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.
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

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.
You can also manually manage the ID attribute by using a class variable.
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 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.
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 class has an incremental cls_id attribute starting at 1 instead of 0.
You can learn more about the related topics by checking out the following tutorials: