Traceback error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Traceback error (/thread-13064.html) |
Traceback error - tjnichols - Sep-26-2018 I'm trying to import a class into another Python file. The previous example I have for this is something that Larz60+ built for me but he didn't set it up this way. In any event, this is what I'm faced with in the example I'm trying to learn. This is the class I've made called "Students.py: class student: def__init__(self, name, major, GPA, is_on_probation): self.name = name self.major = major self.GPA = GPA self.is_on_probation = is_on_probationThis is where we import it: from Students import Students student1 = Student("Jim", "Business", 3.1, False) print(student1)This is the error: As always - any help you can provide is most appreciated!
RE: Traceback error - ThiefOfTime - Sep-26-2018 The Problem is that you are missing spaces between def __init__ and (self, ...) so just replace the line def__init__(self, name, major, GPA, is_on_probation):with def __init__ (self, name, major, GPA, is_on_probation):Maybe you have a little type error there, but I'm a bit surprised that the import is working, because the student class is called "student", but you imported "Students" and used it with "Student" RE: Traceback error - tjnichols - Sep-26-2018 Ok - cool - I've made the changes you've suggested. Now I'm getting another error. My code for students: I've tried changing 'students' with 'student' and either way I get this error. I am using PyCharm and it has a light bulb that suggests 'renaming the element'. What does that mean?: class students: def __init__ (self, name, major, GPA, is_on_probation): self.name = name self.major = major self.GPA = GPA self.is_on_probation = is_on_probationMy code for App. Here, I've tried naming everything to student (except for the file name). Here I get a light bulb suggesting I 'convert to import students'. Which caused another error so I switched it back. On another note, when I change 'from Students import Students' to 'from Students import student' the whole line became grey. Is that the way it should be? from Students import Students student1 = Students("Jim", "Business", 3.1, False) print(student1) I really appreciate your help! Thank you!
RE: Traceback error - ichabod801 - Sep-26-2018 You need to get the capitalization the same as well. In Python, students is different than Students. RE: Traceback error - ThiefOfTime - Sep-26-2018 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_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 RE: Traceback error - tjnichols - Sep-26-2018 Both of you - thank you for your help! icabod801 - thank you for pointing that out. I should have caught that! ThiefOfTime - thank you. Both of these possibilities seemed to work in that they processed with the exit code of 0. It wasn't what I expected though. Here it is: Thank you!
RE: Traceback error - ThiefOfTime - Sep-26-2018 Well, you created an object, if you try to print it this kind of output will occure. if you want a neatly looking output when you convert it to string (or when you want to print it), you need to change the Students.py to this: 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 def __repr__(self): return "{} {} {} {}".format(self.name, self.major, self.GPA, self.is_on_probation)This will let you have an defined output, when you try to print it :) RE: Traceback error - tjnichols - Sep-26-2018 Now that is REALLY COOL! Thank you! That went further than the YouTube video I've been watching. Thanks again! RE: Traceback error - ThiefOfTime - Sep-26-2018 there are some methods each class shares, like __init__ , __repr__ , __str__ , etc. They are very useful and even I need to use them more *shame on me* xD no problem, mate. classes are a huge field, it is worth diving deep into. cheers ;) |