Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Traceback error
#5
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
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_probation
the 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_probation
the 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
Reply


Messages In This Thread
Traceback error - by tjnichols - Sep-26-2018, 12:51 PM
RE: Traceback error - by ThiefOfTime - Sep-26-2018, 01:02 PM
RE: Traceback error - by tjnichols - Sep-26-2018, 02:40 PM
RE: Traceback error - by ichabod801 - Sep-26-2018, 04:38 PM
RE: Traceback error - by ThiefOfTime - Sep-26-2018, 04:56 PM
RE: Traceback error - by tjnichols - Sep-26-2018, 05:10 PM
RE: Traceback error - by ThiefOfTime - Sep-26-2018, 06:02 PM
RE: Traceback error - by tjnichols - Sep-26-2018, 07:13 PM
RE: Traceback error - by ThiefOfTime - Sep-26-2018, 08:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Traceback error tjnichols 3 2,711 Sep-25-2018, 07:24 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020