Section 16: Object-Oriented Programming Flashcards
Define a type:
- Helps regrouping different variables or different types to define our own type
- Part of Object-Oriented Programming
Define
Object:
> 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 to define your own type in Python?
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
Defining your own type:
What is a class
?
> 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 are objects
related to a newly defined class
?
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 is a new class usually defined?
Generally …
Define class using keyword class
as follows:
```python
class MyNewType:
‘’’ a new data type ‘’’
~~~
- Class:
MyNewType
- Full name of the type:
\_\_main\_\_.MyNewType
What is instantiation
of a new object?
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 to add attributes to an object:
- 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
Constructors
What are methods?
Define functions associated with the class they will be defined within the class definition → methods
What is a Constructor?
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\_\_
\_\_init\_\_
Constructor (Initalizer Method)
What is the \_\_init\_\_
constructor?
- 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:
Class Attributes
Instance Attributes
Instance Attributes: Create variables store data specific to an object
- Access through variables through instance (instance = an object of a class)
What are Class Attributes?
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)
Define OOP methods:
> 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:
- Instance methods
- Class methods
- Static Methods
Methods
What are the Instance Methods
and the self
?
- 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)
The \_\_str\_\_
method
What is the relation between the str()
and print()
functions?
- Use function
str
to convert object of built-in types into string - Use function
print
with an object, interally functionstr
is called
What is the \_\_str\_\_
method?
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
General structure for a docstring for a newly defined class:
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
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
What is the difference between the module copy
and deepcopy
?
copy()
and deepcopy()
:
- Function
copy.copy
creates a shallow copy of the input object - Need a deep copy use
copy.deepcopy
instead
What does the import module copy
do?
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
**
Define
Operator Overloading:
> 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
What is the \_\_eq\_\_
method?
Method __eq__ takes two inputs, self and other object, returns a boolean
Operator Overriding
Examples of other default operators:
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!=