Python Udemy Classes
Class
A blue print for an instance
What is Polymorphism?
Two class that have the same method but do actually different things. Dog/Cat Object with attribute get_affection() Dog will bark Cat will purr Another example of this would be len()
What is encapsulation?
Using getters and setters within your class. This gives you more control for what values are set for the instance. This ensures the values are set correctly, and there is data integrity.
What magic method does 'abc' in var?
__contains__
What magic method does var == abc?
__eq__
what magic method does var[1]?
__getitem__
what magic method does var[1:3]?
__getslice__
what magic method does len()?
__len__
Name all 18 Methods for Dictionary?
'clear',- Empty's the Dictionary with no keys and values. 'copy'- Copy a dictionary to another variable 'fromkeys'- To create a dict with a list/tuple for key values and set a key value. dict = dict.fromkeys(my_list) 'get' - Will get return a value when given a key from a dictionary but will return None 'has_key', - Will return True or False if the Key Exist in the dictionary. 'items', - Will return a list with each list element as a tuple with the key, value. [(key, value), (key, value)] 'iteritems', - Used to iterate over a dictionary and can have key and value as a variable. 'iterkeys', 'itervalues', 'keys', - Returns a list with all the keys. 'pop', - This will pop an element in the dictionary by providing the key. Returns the value that was popped. 'popitem', - This will pop an element in the dictionary by providing the key. Returns the value of the key and value as a tuple. 'setdefault', - Will take a Key value and return a value if the key exist. If the key does not exist it will add the key with a None Value and return none. 'update', - Adds a new dict to another dict 'values', - Returns values as a list 'viewitems', - Return a new view of the dictionary's items ((key, value) pairs) 'viewkeys' - Return a new view of the dictionary's keys, 'viewvalues' - Return a new view of the dictionary's values.
What is a Abstract Base Class?
Abstract class is a model for other classes to be designed. It will not actually be initialized only inherited. This requires methods in the child class which is created in the parent abstract class.
Inheriting Class is known as?
Child Class Derived Class Subclass
What is a Class Variable/Attribute? What is an Instance Attribute?
Class Variable/Attribute a variable set in the class below the Class Keyword. Instance Attribute is a attribute in the class using self.someAttribute.
What are the three pillars of OOP Object Oriented Programming?
Encapsulation Inheritance Polymorphism
What is everything in Python?
Everything is a object in Python
What is Composition, why is it preferred over Inheritance?
Inheritance can be brittle if code is changed.
How does Inheritance work?
Inheritance extends attribute lookup to have a class lookup to a parent class Class parent(): def get_foo(self): Class child(parent): mychild = child() mychild.get_foo()
What is __init__?
Its a constructor that allows to setup a object when its initialized while its being created. Init is magic method because it happens automatically when a new instance is created. Its also a private method which is why its underscore__init__(self):.
What is MRO?
Method Resolution Order is used to determine inheritance from multiple classes. In most cases MRO will be depth first unless its a diamond shape of inheritance.
Inherited Class is known as?
Parent Class Base Class Super Class
What is self?
Self represents the instance within the class. Example: Class Food(object): def get_food(self): print 'Here is your food!" pizza = Food()
What does super() actually do?
Super will relate the child class to the parent class. This means the childclass can have its own init and access the parents init together.
How does attribute look up order work?
The attribute lookup order will start with the instance, the class, and then inherited classes.
Where does a instance look for attributes first?
The instance will look in the class it belongs for attributes.
What magic method is used when calling print?
The object uses def __repr__(self):
What does __method__?
These are magic methods that are private within python. This is how Add works and other functions. so 4.__add__(4) would be the same as 4 + 4.
How does with close a file or connection?
With has a magic method called __exit__ which it will close at the end of the block.
Can you inherit a constructor from a parent class?
Yes you can, the child class will not have a init and use the parents init.
Method
a callable attribute defined in a class
Instance
a constructed object of the class
What imports and decorators will need for Abstract Class?
import abc class MyClass(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def some_func(self): return foo
Type
indicates the class the instance belongs to
What is a class method?
its a method that is used within the class but does not use the instance. class MyClass(object): count = 0 def __init__(self): MyClass.count +=1 @classmethod def get_count(cls): return cls.count
What is a static method?
static method changes a instance attribute like a variable but does not use self within the function. Its a utility method used for the class.