8. Object Oriented Programming Flashcards

1
Q

In Python, everything is an ….

A

object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What function is used to check the object’s type?

A

type()

print(type(1))
print(type([]))
print(type(()))
print(type({}))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What keyword creates an object?

A

class

# Create a new object type called Sample
class Sample:
    pass
# Instance of Sample
x = Sample()

print(type(x))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the ‘class’ keyword do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the syntax for creating an attribute?

A

The syntax for creating an attribute is:

self.attribute = something

There is a special method called:
__init__()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the special method used to create an attribute?

A

__init__()

class Dog:
    def \_\_init\_\_(self,breed):
        self.breed = breed
sam = Dog(breed='Lab')
frank = Dog(breed='Huskie')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do call an attribute?

A

sam.breed

class Dog:
    def \_\_init\_\_(self,breed):
        self.breed = breed

sam = Dog(breed=’Lab’)

sam.breed
‘Lab’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Does calling an attribute require parentheses?

A

No

Note how we don’t have any parentheses after breed; this is because it is an attribute and doesn’t take any arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a ‘class object attributes’ ?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

A method is a ?

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you create a method?

A

‘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())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the important benefits of inheritance?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does a class inherit another class?

A
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!")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is polymorphism?

A

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())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the four most common special methods?

A
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")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What defines a special method?

A

The use of underscores. Two underscores before and two after the name.

__init__()
__str__()
__len__()
__del__()