Sep-26-2018, 04:56 PM
(This post was last modified: Sep-26-2018, 04:56 PM by ThiefOfTime.)
Let's break it down step by step ;)
You have a file named Students.py that is the module you are trying to import. Inside of this module you have a class named students (lets rename it to student again, because it fits perfectly). Since you tryied to create an instance of the class by typing Students("Jim", "Business", 3.1, False) the compiler thinks that you have a different class called Students so the students class you loaded from the Students module is not used :) The most important step is to use the class name while importing. If you like you can then even give it a different name, but until this step the names should match. If you have decided to use a one of these possibilities you have to stick with that name, so when you call the constructor you have to use the right name.
(just keep track of when you use capital and when you used lower letters)
Possibility 1:
Students.py
Possibility 2:
Students.py
You have a file named Students.py that is the module you are trying to import. Inside of this module you have a class named students (lets rename it to student again, because it fits perfectly). Since you tryied to create an instance of the class by typing Students("Jim", "Business", 3.1, False) the compiler thinks that you have a different class called Students so the students class you loaded from the Students module is not used :) The most important step is to use the class name while importing. If you like you can then even give it a different name, but until this step the names should match. If you have decided to use a one of these possibilities you have to stick with that name, so when you call the constructor you have to use the right name.
(just keep track of when you use capital and when you used lower letters)
Possibility 1:
Students.py
class student: #this is the class name that we have to use to import it into a different file def __init__ (self, name, major, GPA, is_on_probation): self.name = name self.major = major self.GPA = GPA self.is_on_probation = is_on_probationthe main script
from Students import student #here we use the class name from Students.py to import it into this file student1 = student("Jim", "Business", 3.1, False) #the exact same name has to be used here as the constructor print(student1)Here you see that we use Students (the file name) as where to import from and then tell the computer what to import. Here we want the class student to be imported. When this import is done student has to be used while calling its constructor.
Possibility 2:
Students.py
class student: #still the class name which has to be used when you like to import it def __init__ (self, name, major, GPA, is_on_probation): self.name = name self.major = major self.GPA = GPA self.is_on_probation = is_on_probationthe main script
from Students import student as Student #now we still use the class name from Students.py, but we giving it an alias name. Here Student student1 = Student("Jim", "Business", 3.1, False) #We use the alias now to call the constructor print(student1)Here whe still import from the Students.py file and still want to import the class student that is defined, but we are renaming it (or let's say we are giving it an alias name). In this case Student. Now we call the constructor, but we have to use the alias Student