How are classes and objects related(4)?
Compare subclass vs superclass
Subclasses are more specialized, superclass is more general.
What is inheritance as it relates to superclass/subclass?
Passing attributes and methods from the superclass (defined and existing) to a newly created class, called the subclass.
What is an object comprised of(3)?
Write the code to create a class and object:
class SampleClass:
pass
SampleObject = SampleClass()
What is instantiation?
An object being created and becoming an instance of the class.
Cons of the procedural approach (the stack)(2):
Benefits of the object oriented approach (the stack)(2):
What is a constructor? What is its purpose? What is the constructors name?
What is the parameter that must be used with __init__
self
example
class SampleClass:
def __init__(self):
print(“Hola!”)
Explain the below code:
class Stack:
def __init__(self):
self.stackList = []
The “Stack” class has a constructor so that objects can be made. The “stackList” property has been added to the new object, it is ready to use its value.
What do two underscores signify before a class component name? Where can this be accessed? What is this called?
The class component becomes private. It can be accessed only from within the class. This is encapsulation.
When functions are created, why must a parameter named “self” be present?
Write the code to create two stacks:
class Stack:
def __init__(self):
self.__stackList = []
def push(self, val):
self.\_\_stackList.append(val)
def pop(self):
val = self.\_\_stackList[-1]
del self.\_\_stackList[-1]
return valstackObject1 = Stack()
stackObject2 = Stack()
- Two stacks created from the same base class, they work independently.
Based upon the below code, how are Stack and AddingStack related?
class Stack:
pass
class AddingStack(Stack):
pass
Stack is the superclass while AddingStack is the subclass. AddingStack gets all the components defined by its superclass.
What does the Stack.__init__(self) line do below?
class Stack:
def __init__(self):
self.__stackList = []
def push(self, val):
self.\_\_stackList.append(val)
def pop(self):
val = self.\_\_stackList[-1]
del self.\_\_stackList[-1]
return valclass AddingStack(Stack):
def __init__(self):
Stack.__init__(self)
self.__sum = 0
It explicitly invokes the superclass’s constructor. Otherwise the class wouldn’t have the __stackList property.
True or False:
Invoking any method from inside the class never requires you to put the self argument at the arguments list
False
Invoking any method from OUTSIDE the class never requires you to put the self argument at the argument’s list
What are instance variables?
What instance variables to the objects below contain:
class ExampleClass:
def __init__(self, val = 1):
self.first = val
def setSecond(self, val):
self.second = valexampleObject1 = ExampleClass()
exampleObject2 = ExampleClass(2)
exampleObject2.setSecond(3)
exampleObject3 = ExampleClass(4)
exampleObject3.third = 5
exampleObject1 has the first variable
example Object 2 has the first and second variables
example Object 3 has the first and third variables
What is the __dict__ variable?
Info about Class Variables. Where is it stored? Does it exist even when there are no objects?
True or False:
The __dict__ is empty if the object has no instance variables
True
What does the hasattr function do? What are its arguments? What does it return?
What is the output of:
class ExampleClass:
a = 1
def ExampleClass():
def __init__(self):
self.b = 2
exampleObject = ExampleClass()
print(hasattr(exampleObject, ‘b’))
print(hasattr(exampleObject, ‘a’))
print(hasattr(ExampleClass, ‘b’))
print(hasattr(ExampleClass, ‘a’))
True
True
False
True