Define a type:
Define
Object:
> An object is a collection of data and set of methods can be provided to work with it
OOP: object oriented programming
How to define your own type in Python?
In Python: can define own data type
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
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
How is a new class usually defined?
Generally …
Define class using keyword class as follows:
```python
class MyNewType:
‘’’ a new data type ‘’’
~~~
MyNewType\_\_main\_\_.MyNewTypeWhat 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:
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?
\_\_init\_\_```python
def__init__(self):
~~~
self refers to the object being initalized, not a keywordClass Attributes
Instance Attributes
Instance Attributes: Create variables store data specific to an object
What are Class Attributes?
Class Attributes:
Define class attributes inside class → outside all methods
Define OOP methods:
> Define functions associated with the class they will be defined within the class definition → methods
Type:
Methods
What are the Instance Methods
and the self?
Convention: name self (not a keyword)
The \_\_str\_\_ method
What is the relation between the str() and print() functions?
str to convert object of built-in types into stringprint with an object, interally function str is calledWhat is the \_\_str\_\_ method?
The \_\_str\_\_ method:
\_\_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?
Copying Objects:
module copy and use to copy a functioncopy() and deepcopy():
copy.copy creates a ****shallow**** copy of the input objectcopy.deepcopy insteadWhat is the difference between the module copy and deepcopy?
copy() and deepcopy():
copy.copy creates a shallow copy of the input objectcopy.deepcopy insteadWhat 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
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 !=