Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to create an infinite sequence of ids in python?

"""
We have a Student class. Every
student has a name and an id.
The catch is that a student id is 
automatically set to next available one.
"""
from itertools import count

class Student:
    # id generator starting from 1
    student_id_generator = count(1)  # first_id=1

    def __init__(self, name):
        self.name = name
        # Generate next student's id
        self.id = next(Student.student_id_generator)

student1 = Student("Wissam")
student2 = Student("Fawzi")
print(student1.id)  # 1
print(student2.id)  # 2
Comment

PREVIOUS NEXT
Code Example
Python :: pandas percentage change across multiple periods 
Python :: rotation points space python 
Python :: orderd dictionary pop vs del 
Python :: how to show process bar in terminal python 
Python :: qlineedit autocomplete python 
Python :: python convert twitter id to date 
Python :: price for bazaar item hypixel python 
Python :: pandas dataframe from multiple csv 
Python :: last 24 hour python datetime 
Python :: split every character python 
Python :: pyqt5 message box 
Python :: replace column values pandas 
Python :: python get date tomorrow 
Python :: convert categorical variable to numeric python 
Python :: ctx.save_for_backward 
Python :: how to get total number of rows in listbox tkinter 
Python :: python pandas convert nan to 0 
Python :: bring tkinter window to front 
Python :: pandas replace empty string with nan 
Python :: create df from two arrays 
Python :: keras auc without tf.metrics.auc 
Python :: python detect color on screen 
Python :: plt.savefig without showing 
Python :: add element to heap python 
Python :: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaple 
Python :: python sum of digits in a string 
Python :: how to set required drf serialzier 
Python :: python negative infinity 
Python :: call materialized view in django postgres 
Python :: pandas extract month year from date 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =