Quiz 5 Material Flashcards
classes create ______ that encapsulate both data and methods
objects
variables + methods = ________
attributes
what is the return type of math.floor()?
a math object
what is math.ceil() called?
a constructor
can you import classes?
yes
how would you write the heading for a class called MyClass? AKA class definition
class MyClass:
what is an instance/object of a class?
it is one particular creation of the class (kind of like a recipe)
what is the first method that is in the class and how is it written?
- it is the constructor that is call when you create an object of the class
def __init__(self, parameters)
public vs private attributes
public attributes - which are open to access by anyone
private attributes - which are only available to the class object
accessor methods
providing some public means of accessing private elements of our class
write a private and public attribute for charge?
private: self.__charge
public: self.charge
the __str__ method
it is common practice to include a method that provides a representation of an object as a string
are the __init__ method and the __str__ method called by invoking their actual names?
no
if we don’t use self, then the variable only exists in the scope of the particular ________
function
does self have to be the first parameter?
yes
steps for writing to a file
- open a file for writing – outfile = open(filename, ‘w’) OR with open(filename,’w’) as outfile:
- write to the file – outfile.write() OR print(…, file=outfile)
- close the file – outfile.close()
what is the difference between opening a file in ‘w’ mode and opening a file in ‘a’ mode
‘w’ mode - OS will erase everything that was already there
‘a’ mode - OS will append to existing file