L
L
Lesha2016-07-14 18:08:29
Python
Lesha, 2016-07-14 18:08:29

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)

Not quite written to the end, but the problem is this:
def add_student(name, age, sclass, info=None):
    new_student = Student()  # create instance class(new student)

How to pass function arguments to arguments to create a duplicate class?
I tried that, it didn't work. writes Unexpected argument in the second line:
add_student(name, age, sclass, info=None):
    new_student = Student(name, age, sclass, info)  # create instance class(new student)

I hope for your help)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danil Biryukov-Romanov, 2016-07-14
@enempluie

class Student:
    def __main__(self, name, age, sclass, info):

Replace with
class Student:
    def __init__(self, name, age, sclass, info):

And everything will work - the Unexpected argument error will disappear.
You incorrectly named the class initializer function

S
sim3x, 2016-07-14
@sim3x

stackoverflow.com/questions/625083/python-init-and...
__main__ -> __init__

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question