Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialInes Fazlić
Python Web Development Techdegree Student 9,569 Pointshow does the calling of Book() work if not imported?
I was just wondering how does the calling of Book class in Bookcase work if Kenneth imported like this: from books import Bookcase?
Can someone please explain? we probably already covered this but it must of slipped through the cracks of my mind lol
2 Answers
Alexander Davison
65,469 PointsIf you import something (may it be a function, a class, etc.), and it uses a piece of data (it could be a variable, a function, a class, etc.) from the program it's from, you still can import it alone and it correctly looks for the variables in the original program it is imported from.
So, yes, from books import Bookcase
is completely valid, and Bookcase
may access Books
from the original program it was from. However, if you defined a variable, then imported Bookcase
frombooks
, Bookcase
would not be able to access it.
For example:
# This is my_program.py
my_variable = 123
def my_func():
print(my_variable)
# This is test.py
from my_program import my_func
my_func() # Prints 123
my_variable = 64
my_func() # Still prints 123
I hope this helps! ~Alex
Ines Fazlić
Python Web Development Techdegree Student 9,569 PointsThank you Alex! totally helped, I get it now :)
Alexander Davison
65,469 PointsNo problem :D
Jay Reyes
Python Web Development Techdegree Student 15,937 PointsThank you Alex, I'm actually quite surprised Kenneth skipped over this critical aspect of classes. If he talked about it in a previous video, feel free to refer me.