Equivalent Julia code for a Python class with counter variables

Consider the following Python code from the link

# Python code
class Student:
    # A student ID counter
    idCounter = 0
    def __init__(self):
        self.gpa = 0
        self.record = {}
        # Each time I create a new student, the idCounter increment
        Student.idCounter += 1
        self.name = 'Student {0}'.format(Student.idCounter)

classRoster = [] # List of students
for number in range(25):
    newStudent = Student()
    classRoster.append(newStudent)
    print(newStudent.name)

What would be the best way to implement this equivalently in Julia, preferably using mutable struct? Any tips/best practices will be much appreciated.

This is one possibility:

mutable struct Student
    gpa::Int
    record::Dict{Any, Any}
    name::String
end

let counter = 0
    global function Student()
        counter += 1
        return Student(0, Dict(), "Student $counter")
    end
end
1 Like

Thanks, that works.