OOP basics Flashcards
What is the main point of OOP in Python?
OOP is about code reuse — you factor code to minimize redundancy and program by customizing what already exists instead of changing code in place or starting from scratch.
Where does an inheritance search look for an attribute?
An inheritance search looks for an attribute first in the instance object, then in the class the instance was created from, then in all higher superclasses, progressing from left to right (by default). The search stops at the first place the attribute is found.
What is the difference between a class object and an instance object?
Classes are a kind of factory for creating multiple instances. Classes also support operator overloading methods, which instances inherit, and treat any functions nested in the class as methods for processing instances.
Why is the first argument in a class’s method function special?
It always receives the instance object that is the implied subject of the method call. It’s usually called ‘self’ by convention.
What is the __init__ method used for?
If the \_\_init\_\_ method is coded or inherited in a class, Python calls it automatically each time an instance of that class is created. class New(): def \_\_init\_\_(self, arg1, arg2): self.first_var = arg1 self.second_var = arg2
How do you create a class instance?
You create a class instance by calling the class name as though it were a function; any arguments passed into the class name show up as arguments two and beyond in the __init__ constructor method.
x = ClassName() y = AnotherClass(arg1, arg2)
How do you create a class?
class ClassName(): #some code here
What is inheritance?
Inheritance is when a class is based on another class or classes, getting the same methods and attributes from them.
What is composition?
Composition is when the class-container has in it calls to other classes instead of inheriting from them. So after object is created from container-class this object also composes of objects made by internal calls of included classes.
What is a class?
Class serves as instance (object) factory. It provide attributes (variables) and methods (functions) that are inherited by all the instances generated from each classes call.
What is an instance?
Instance is a single item (object) created once by each class call.
What are superclasses?
They are classes which are used to inherit from.
class Son(Father, Mother): …
In this case Father and Mother are superclasses for Son subclass.
What are superclasses?
They are classes which are used to inherit from.
class Son(Mother, Father): …
In this case Mother and Father are superclasses for Son subclass.
What are subclasses?
They are classes which inherit from parent superclasses their methods and attributes.
class Son(Mother, Father): …
In this case Mother and Father are superclasses for Son cubclass.
What happens if subclass has it’s own attrs and methods like in it’s superclasses?
Own subclasses attrs and methods have higher priority than that from their parents and should replace and override them.
class Mother(): def \_\_init\_\_(self): pass passion_to = 'boys' def like(self): print 'I like ' + self.passion_to
class Son(Mother): def \_\_init\_\_(self): pass passion_to = 'girls' def like(self): print 'I LOVE ' + self.passion_to
john = Son()
john.like() # –> ‘I LOVE girls’