Mod 6 Flashcards
How are classes and objects related(4)?
- A class is a set of objects.
- An object belongs to a class.
-An object is an incarnation of the requirements, traits and qualities assigned to a class. - Classes do not need an object to exist
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)?
- name that uniquely defines it (noun - car)
- set of individual attributes (adjective - red)
- set of abilities to perform specific actives (verb - drive)
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):
- The stack list or variable is highly vulnerable, anyone can modify it in a uncontrolled manner
- if you want multiple stacks, cannot easily duplicate them, confusing code
Benefits of the object oriented approach (the stack)(2):
- Can encapsulate values so they can be neither accessed nor modified
- do not need to duplicate code to create multiple stacks
What is a constructor? What is its purpose? What is the constructors name?
- A function that is invoked implicitly and automatically.
- Its purpose is to construct a new object.
- The name is __init__.
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?
- It allows the method to access the object’s entities
- A method it needs to have least one parameter, which is used by Python
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 val
stackObject1 = 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 val
class 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?
- They are variables created at the object level.
- They can possess different set of properties.
- They can be created within the constructor.
- Modifying an instance variable has no impact on all the remaining objects.
- These only exist when there is no object in the class.
What instance variables to the objects below contain:
class ExampleClass:
def __init__(self, val = 1):
self.first = val
def setSecond(self, val): self.second = val
exampleObject1 = 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?
- Returns a dictionary
- It contains all of the names and values of all the properties (variables) the object is currently carrying.
Info about Class Variables. Where is it stored? Does it exist even when there are no objects?
- It’s stored outside any object.
- A class variable exists in one copy even if there are no objects in the class.
- They always show the same value in all class instances
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?
- Checks if any object/class contains a specified property (variable).
- It needs two arguments, the class or object and the name of the property.
- It returns True or False
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
What is a method? Is a parameter required? Convention?
- It is a function embedded inside a class
- must have at least one parameter
- convention is to use “self” as the parameter
Facts about “self” parameter(2):
- Gain access to an object’s instance and class variables
- Used to invoke other object/class methods from inside the class
Facts about the constructor(3):
Parameter (2)?
- it is obliged to have the self parameter
- doesn’t need to have more parameters
- can be used to set up the object
- cannot return a value
- cannot be invoked directly either from the object or from inside the class
Write the code to access the hidden method name “__hidden”:
class Classy:
def visible(self):
print(“visible”)
def \_\_hidden(self): print("hidden")
obj = Classy()
obj._Classy__hidden()
What does the __name__ property (variable) do?
It contains the name of the class. It exists only inside classes.
What does the type() function do (in the context of classes/objects)?
It will find the class of a particular object.
Write the code that will return the name of the class:
class Classy:
pass
print(Classy.__name__)
Write the code that will return the name of the class:
class Classy:
pass
obj = Classy()
print(type(obj).__name__)
What does the __module__ attribute do?
It stores the name of the module which contains the definition of the class
What does the __bases__ attribute do?
It is a tuple that contains a list of all the classes the given class inherits. Only classes have this attribute. A class without explicit superclasses points to “object”.
What is introspection?
the ability of a program to examine the type or properties of an object at runtime
What is reflection?
the ability of a program to manipulate the values, properties and/or functions of an object at runtime.
True or False:
Reflection and introspection enable a programmer to do anything with every object, no matter where it comes from.
True
What does the __str__() function do?
It returns a string. Helpful for when the class/object output needs to be a string.
What is the name and syntax of the function that is able to identify the relationship between two classes? What does it return?
issubclass(ClassOne, ClassTwo)
- it returns True if ClassOne is a subclass of ClassTwo and False otherwise
Note: each class is considered to be a subclass of itself