This community-built FAQ covers the “Class Based Context Managers II” exercise from the lesson “Context Managers”.
Paths and Courses
This exercise can be found in the following Codecademy content:
FAQs on the exercise Class Based Context Managers II
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
I like this lesson, but I have a question concerning the scope of variables in the class. To be more specific concerning the scope of a variable that is defined in a class method. My question might be related to OOP.
For context please check the following code and look for the variable self.opened_poem_file
Write your code below:
class PoemFiles:
def __init__(self, poem_file, mode):
print('Starting up a poem context manager')
self.file = poem_file
self.mode = mode
def __enter__(self):
print('Opening poem file')
self.opened_poem_file = open(self.file, self.mode)
return self.opened_poem_file
def __exit__(self, *exc):
print('Closing poem file')
self.opened_poem_file.close()
with PoemFiles('poem.txt', 'w') as open_poem_file:
open_poem_file.write('Hope is the thing with feathers')
The variable self.opened_poem_file is defined within the instance method enter() and it is accessed by the instance method exit().
My questions are as follow:
Why do I need to write the “self.” keyword in front of the variable?
I tried leaving the keyword out and got a NameError, as the exit() method didn’t recognize the variable.
So I guess it has to be an instance variable for the exit() method to be able to access it.
How is it possible to define instance variables outside of the init() method?
I used to think that instance variables only can be defined inside the init() mmethod.
But if I can define instance variables in any other method, what is the init() actualy for?
Take in the passed arguments?
The init() method will define the variables that are required to initiate the class. Sometimes you will need other variables to call additional methods, but you may have a class that doesn’t need every defined method to be used so you wouldn’t want to have to define every variable to initiate the class.
“Self” is required to create an instance variable. Instance variables create distinct values for each object. If you didn’t use an instance variable, the value for every iteration of this class would be the same. “Self” allows you to pass in an object-specific value and carry it from method to method.