Python Flashcards
What does this operator do %?
returns a remainder
What does this operator do //?
math.floor(a / b)
What happens when a subclass inherits from a base class?
The subclass inherits all the methods, properties, and attributes of the base class.
What are some of the features/advantages of python
Supports multiple inheritance.
What is multiple inheritance?
Multiple inheritance is the ability to derive a class from multiple base classes at the same time. Multiple inheritance has a bad reputation to the extent that most modern programming languages don’t support it. Instead, modern programming languages support the concept of interfaces. In those languages, you inherit from a single base class and then implement multiple interfaces, so your class can be re-used in different situations.
what is pythons MRO?**Hint multiple inheritance
Method resolution order. The MRO shows the order of classes in which Python is going to look for a matching attribute or method. it can be accessed on the class attribute .__mro__ we can use this to troubleshoot multiple inheritance problems.
What are some disadvantages of multiple inheritance?
1) We can end up creating tightly coupled relationships that become too dependent on each other. 2) Create complex relationships between classes that become hard to follow and debug. 3) It can create redundancy if two classes inherit from the same base class. The opposite approach would be to do a composition design. loosely coupled, flexible design.
What is __str__( ) method in python?
It provides a verbose, readable string representation of your class. invoked when print( ). is called with an object. it’s considered unofficial documentation str() is used for creating output for end-user while repr() is mainly used for debugging and development. repr’s goal is to be developer-friendly and ideally shows how to recreate the object and str’s meant to be user friendly and readable
What is Liskovs substitution principle in relation to python?**HINT THIS HAS TO DO WITH INHERITANCE.
Liskov’s substitution principle is the most important guideline to determine if inheritance is the appropriate design solution. Inheritance should only be used to model an “is a” relationship. Liskov’s substitution principle says that an object of type Derived, which inherits from Base, can replace an object of type Base without altering the desirable properties of a program.
what is the __dict__ attribute in python?
Creating a class implicitly inherents from object . __dict__ is one of the members of the object class and is basically. a mapping of all the attributes in an object to their value.
What does dictionary comprehension syntax look like?
{i : chr(65+i) for i in range(4)}
what’s the difference between item( ) and iteritem( ) on a python dictionary?
The difference only matters In python 2. in python 3 items( ) Return an iterator over the dictionary’s (key, value) pairs. we use this to iterate over the dictionary
When should you use inheritance? When should you use them over composition?
1) Use inheritance over composition in Python to model a clear “IS A” relationship. 2) Use inheritance over composition in Python to leverage BOTH the interface and implementation of the base class. 3) Use inheritance over composition in Python to provide mixin features to several unrelated classes when there is only one implementation of that feature.
If you’re able to justify an inheritance between two classes both ways they probably should not derive from each other.
describe isinstance( ) in python?
Return True if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If the object is not an object of the given type, the function always returns False.
What is the __repr__( ) method in python?
__repr__() compute the “official” string representation of an object (a representation that has all information about the object). Used for debugging and should be wrapped in backquotes.
When should you use composition over inheritance?
1) Use composition over inheritance in Python to model a has a relationship that leverages the implementation of the component class. 2) Use composition over inheritance in Python to create components that can be reused by multiple classes in your Python applications. 3) Use composition over inheritance in Python to implement groups of behaviors and policies that can be applied interchangeably to other classes to customize their behavior.