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)