8. Object Oriented Programming Flashcards
In Python, everything is an ….
object
What function is used to check the object’s type?
type()
print(type(1))
print(type([]))
print(type(()))
print(type({}))
What keyword creates an object?
class
# Create a new object type called Sample class Sample: pass
# Instance of Sample x = Sample()
print(type(x))
What does the ‘class’ keyword do?
User defined objects are created using the class keyword. The class is a blueprint that defines the nature of a future object. From classes we can construct instances. An instance is a specific object created from a particular class. For example, above we created the object lst which was an instance of a list object.
What is the syntax for creating an attribute?
The syntax for creating an attribute is:
self.attribute = something
There is a special method called:
__init__()
What is the special method used to create an attribute?
__init__()
class Dog: def \_\_init\_\_(self,breed): self.breed = breed
sam = Dog(breed='Lab') frank = Dog(breed='Huskie')
How do call an attribute?
sam.breed
class Dog: def \_\_init\_\_(self,breed): self.breed = breed
sam = Dog(breed=’Lab’)
sam.breed
‘Lab’
Does calling an attribute require parentheses?
No
Note how we don’t have any parentheses after breed; this is because it is an attribute and doesn’t take any arguments.
What is a ‘class object attributes’ ?
In Python there are also class object attributes. These Class Object Attributes are the same for any instance of the class. For example, we could create the attribute species for the Dog class. Dogs, regardless of their breed, name, or other attributes, will always be mammals. We apply this logic in the following manner:
class Dog:
# Class Object Attribute species = 'mammal'
def \_\_init\_\_(self,breed,name): self. breed = breed self. name = name
A method is a ?
Methods are functions defined inside the body of a class.
They are used to perform operations with the attributes of our objects. Methods are a key concept of the OOP paradigm. They are essential to dividing responsibilities in programming, especially in large applications.
How do you create a method?
‘def’ keyword … name of method … (self, arguments)
class Circle: pi = 3.14
# Circle gets instantiated with a radius (default is 1) def \_\_init\_\_(self, radius=1): self.radius = radius self.area = radius * radius * Circle.pi
# Method for resetting Radius def setRadius(self, new_radius): self.radius = new_radius self.area = new_radius * new_radius * self.pi
# Method for getting Circumference def getCircumference(self): return self.radius * self.pi * 2
c = Circle()
print(‘Radius is: ‘,c.radius)
print(‘Area is: ‘,c.area)
print(‘Circumference is: ‘,c.getCircumference())
What are the important benefits of inheritance?
Important benefits of inheritance are code reuse and reduction of complexity of a program. The derived classes (descendants) override or extend the functionality of base classes (ancestors).
How does a class inherit another class?
class Animal: def \_\_init\_\_(self): print("Animal created")
def whoAmI(self): print("Animal")
def eat(self): print("Eating")
class Dog(Animal): # <= The inherit class is place in the parenthesis. def \_\_init\_\_(self): Animal.\_\_init\_\_(self) print("Dog created")
def whoAmI(self): print("Dog")
def bark(self): print("Woof!")
What is polymorphism?
In Python, polymorphism refers to the way in which different object classes can share the same method name, and those methods can be called from the same place even though a variety of different objects might be passed in.
class Dog: def \_\_init\_\_(self, name): self.name = name
def speak(self): return self.name+' says Woof!'
class Cat: def \_\_init\_\_(self, name): self.name = name
def speak(self): return self.name+' says Meow!'
niko = Dog('Niko') felix = Cat('Felix')
print(niko.speak())
print(felix.speak())
What are the four most common special methods?
class Book: def \_\_init\_\_(self, title, author, pages): print("A book is created") self.title = title self.author = author self.pages = pages
def \_\_str\_\_(self): return "Title: %s, author: %s, pages: %s" %(self.title, self.author, self.pages)
def \_\_len\_\_(self): return self.pages
def \_\_del\_\_(self): print("A book is destroyed")