Section 16: Object-Oriented Programming Flashcards

1
Q

Define a type:

A
  • Helps regrouping different variables or different types to define our own type
  • Part of Object-Oriented Programming
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define

Object:

A

> An object is a collection of data and set of methods can be provided to work with it

  • Python: object-oriented language.
    Means that it uses objects to represent data and provided methods related to them
  • OOP way to use programmer-defined data types to organize code and data

OOP: object oriented programming

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

How to define your own type in Python?

A

In Python: can define own data type

  • Combine related pieces of information with each other into one variable

Programmer-defined type is also called a class

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

Defining your own type:

What is a class?

A

> Programmer-defined type is also called a class

class keyword defined in Python

  • When define a class, you can actually defining new type

Known classes: int, str, list, dict
```python
»> int
<class ‘int’>
~~~

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

How are objects related to a newly defined class?

A

When you define a new class we create a new object type with the same name

  • A class is a blueprint/template for a type of objectSpecifies attributes and what methods can operate on them
  • An object is an instance of some class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How is a new class usually defined?

A

Generally …

Define class using keyword class as follows:

```python
class MyNewType:
‘’’ a new data type ‘’’
~~~

  • Class: MyNewType
  • Full name of the type: \_\_main\_\_.MyNewType
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is instantiation of a new object?

A

Creating a new object is called instantiation, we say the object is an instance of the class

Call the class as if it were a function

```python
my_object = MyNewType()
~~~

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

How to add attributes to an object:

A
  • Create variables that belong to specific object referenced by student_Bob
  • Attributes only access through objects

Assign values to an object using the dot notation

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

Constructors

What are methods?

A

Define functions associated with the class they will be defined within the class definition → methods

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

What is a Constructor?

A

Constructor: (initalizer method) a method that creates a new object from the class

Constructor: special method when defining a class helps create a new object of that class → \_\_init\_\_

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

\_\_init\_\_ Constructor (Initalizer Method)

What is the \_\_init\_\_ constructor?

A
  • Constructor is a special method named \_\_init\_\_
    • Involved automatically when object is created
  • Used to define attributes of a class t oset initial value

```python
def__init__(self):
~~~

  • self refers to the object being initalized, not a keyword
  • Can have a default value:
- Rename (constructor) `create_student` as `init` - add another input parameter `self` that refers to the object that is being initialized - Call the `init` constructor and provide parameters The first input argument `self` corresponds with the new object being created
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Class Attributes

Instance Attributes

A

Instance Attributes: Create variables store data specific to an object

  • Access through variables through instance (instance = an object of a class)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are Class Attributes?

A

Class Attributes:

  • Create variable store data related to entire class
  • Instance shares the same class attributes

Define class attributes inside class → outside all methods

  • Convention: place vars at top, below class header
  • Access class attributes through instance and through class name (only modify through class name)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Define OOP methods:

A

> Define functions associated with the class they will be defined within the class definition → methods

  • Can define methods the say way as defined functions inside a module

Type:

  1. Instance methods
  2. Class methods
  3. Static Methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Methods

What are the Instance Methods

and the self?

A
  • Calling a method on an object is equivalent to passing that object as input to the method implicity
  • TO indicate this, first argumenet of every instance method is always a reference to an instance of the class

Convention: name self (not a keyword)

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

The \_\_str\_\_ method

What is the relation between the str() and print() functions?

A
  • Use function str to convert object of built-in types into string
  • Use function print with an object, interally function str is called
17
Q

What is the \_\_str\_\_ method?

A

The \_\_str\_\_ method:

  • change representation of our class object by implementing method called \_\_str\_\_() in our class.

Header:

```python
def __str__ (self):
#must return a string
~~~

If you do that → when call print with instance of the class, this method is called automatically

18
Q

General structure for a docstring for a newly defined class:

A
19
Q

What is the alternative to creating a shallow copy like this one below?

  • Aliasing (copying references) as above, only make program hard to read and more pronne to bugs
A

Copying Objects:

  • Aliasing (copying references) as above, only make program hard to read and more pronne to bugs
  • Import module copy and use to copy a function

copy() and deepcopy():

  • Function copy.copy creates a ****shallow**** copy of the input object
  • Need a deep copy use copy.deepcopy instead
20
Q

What is the difference between the module copy and deepcopy?

A

copy() and deepcopy():

  • Function copy.copy creates a shallow copy of the input object
  • Need a deep copy use copy.deepcopy instead
21
Q

What does the import module copy do?

A

When create a copy, new object contains the same data

**- Expect that the operator == would have compared data stored inside object
- By default, == simply compares identities of two objects

Default behavior of == is the same as the operator is**

22
Q

Define

Operator Overloading:

A

> Operator overloading refers to changing behavior of built-in operator, so works as we wish with programmer-defined data types

  • Corresponding special method we can add to our classes to modify operator’s behavior

Modify behavior of ==: define the method __eq__ inside our class

23
Q

What is the \_\_eq\_\_ method?

A

Method __eq__ takes two inputs, self and other object, returns a boolean

24
Q

Operator Overriding

Examples of other default operators:

A

Override many kinds of default operators:

  • \_\_It\_\_: less than operator
  • \_\_gt\_\_: greater than operator
  • \_\_le\_\_ / \_\_ge\_\_: less than or equal to / greater than or equals to
  • \_\_eq\_\_ / \_\_ne\_\_: equal to ‘==’ not equal to !=