Answer the question
In order to leave comments, you need to log in
Duplicate class function?
Hello again, Toaster.
Deciding to create a simple program, I ran into a number of small difficulties.
1. Saving a duplicate class with all its attributes (decided using pickle)
2. Creating a duplicate class using a function (the user of the program selects an action, writes the necessary data,
which will later go into the arguments of the function, which, in turn, will create a duplicate class)
Select action -> add student -> enter data about student (name, age, class, information) -> save data in variables of the same name -> call the function to create a student (duplicate class) and pass data as arguments - saving a duplicate class using pickle .
class Student:
def __main__(self, name, age, sclass, info):
self.name = name
self.age = age
self.sclass = sclass
self.information = info
# код ниже в отдельном модуле, тут пишу всё сразу
import pickle # importing for AddStudent
"""ADD STUDENT BLOCK"""
students_txt_files = []
last_count_txt = open('last_count.txt', 'w')
last_count_txt.write('0')
last_count_txt.close()
def add_student(name, age, sclass, info=None):
new_student = Student() # create instance class(new student)
default_txt_tamplate = 'student_' # create student text file template
last_count = open('last_count.txt') # opening text file with last count number
count = int(last_count.read(1)) # writing last count number to count
student_name_txt = default_txt_tamplate[0] + str(count) # creating new student text file name
student_txt = open(student_name_txt, 'w') # creating new student text file
pickle.dump(new_student, student_txt, 3) # save instance class(student)
def add_student(name, age, sclass, info=None):
new_student = Student() # create instance class(new student)
add_student(name, age, sclass, info=None):
new_student = Student(name, age, sclass, info) # create instance class(new student)
Answer the question
In order to leave comments, you need to log in
class Student:
def __main__(self, name, age, sclass, info):
class Student:
def __init__(self, name, age, sclass, info):
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question