Update tech_docs/python/Python_programming.md
This commit is contained in:
@@ -1,3 +1,149 @@
|
||||
Object-oriented programming (OOP) is a paradigm that organizes software design around data, or objects, rather than functions and logic. The key concepts of OOP are encapsulation, abstraction, inheritance, and polymorphism. Understanding how to relate OOP to real-world problems involves thinking about the entities involved as objects with properties (attributes) and behaviors (methods). Here’s a guide to help you understand and apply OOP principles effectively.
|
||||
|
||||
### Key Concepts of OOP
|
||||
|
||||
1. **Encapsulation**: Bundling data (attributes) and methods (functions) that operate on the data into a single unit, or class.
|
||||
2. **Abstraction**: Hiding complex implementation details and exposing only the necessary parts.
|
||||
3. **Inheritance**: Creating new classes that inherit attributes and methods from existing classes.
|
||||
4. **Polymorphism**: Allowing methods to do different things based on the object it is acting upon.
|
||||
|
||||
### Thinking in Terms of Objects
|
||||
|
||||
#### Step-by-Step Guide
|
||||
|
||||
1. **Identify the Entities**:
|
||||
- Look at the problem you’re trying to solve and identify the key entities involved. These entities will become your objects.
|
||||
|
||||
2. **Define Classes**:
|
||||
- For each entity, define a class. A class is a blueprint for creating objects.
|
||||
|
||||
3. **Determine Attributes and Methods**:
|
||||
- Identify what attributes (properties) each entity should have. These are typically nouns that describe the entity.
|
||||
- Identify what methods (behaviors) each entity should have. These are typically verbs that describe what the entity can do.
|
||||
|
||||
4. **Establish Relationships**:
|
||||
- Determine how the objects will interact with each other.
|
||||
- Use inheritance if there are hierarchical relationships (e.g., a Dog class that inherits from an Animal class).
|
||||
- Use composition if objects contain other objects (e.g., a Car class that contains an Engine object).
|
||||
|
||||
#### Example: Library System
|
||||
|
||||
Let's walk through an example of designing a simple library system.
|
||||
|
||||
1. **Identify the Entities**:
|
||||
- Book
|
||||
- Member
|
||||
- Library
|
||||
|
||||
2. **Define Classes**:
|
||||
|
||||
```python
|
||||
class Book:
|
||||
def __init__(self, title, author, isbn):
|
||||
self.title = title
|
||||
self.author = author
|
||||
self.isbn = isbn
|
||||
self.is_checked_out = False
|
||||
|
||||
def check_out(self):
|
||||
if not self.is_checked_out:
|
||||
self.is_checked_out = True
|
||||
return True
|
||||
return False
|
||||
|
||||
def return_book(self):
|
||||
if self.is_checked_out:
|
||||
self.is_checked_out = False
|
||||
return True
|
||||
return False
|
||||
|
||||
class Member:
|
||||
def __init__(self, name, member_id):
|
||||
self.name = name
|
||||
self.member_id = member_id
|
||||
self.borrowed_books = []
|
||||
|
||||
def borrow_book(self, book):
|
||||
if book.check_out():
|
||||
self.borrowed_books.append(book)
|
||||
return True
|
||||
return False
|
||||
|
||||
def return_book(self, book):
|
||||
if book in self.borrowed_books:
|
||||
if book.return_book():
|
||||
self.borrowed_books.remove(book)
|
||||
return True
|
||||
return False
|
||||
|
||||
class Library:
|
||||
def __init__(self):
|
||||
self.books = []
|
||||
self.members = []
|
||||
|
||||
def add_book(self, book):
|
||||
self.books.append(book)
|
||||
|
||||
def add_member(self, member):
|
||||
self.members.append(member)
|
||||
|
||||
def find_book(self, title):
|
||||
for book in self.books:
|
||||
if book.title == title:
|
||||
return book
|
||||
return None
|
||||
|
||||
def find_member(self, member_id):
|
||||
for member in self.members:
|
||||
if member.member_id == member_id:
|
||||
return member
|
||||
return None
|
||||
```
|
||||
|
||||
3. **Determine Attributes and Methods**:
|
||||
- `Book` attributes: title, author, isbn, is_checked_out
|
||||
- `Book` methods: check_out, return_book
|
||||
- `Member` attributes: name, member_id, borrowed_books
|
||||
- `Member` methods: borrow_book, return_book
|
||||
- `Library` attributes: books, members
|
||||
- `Library` methods: add_book, add_member, find_book, find_member
|
||||
|
||||
4. **Establish Relationships**:
|
||||
- The `Library` class contains `Book` and `Member` objects.
|
||||
- Members can borrow and return books from the library.
|
||||
|
||||
### Applying OOP Principles
|
||||
|
||||
#### Encapsulation
|
||||
- Grouping related attributes and methods into a single class (e.g., `Book` class encapsulates book-related data and behaviors).
|
||||
|
||||
#### Abstraction
|
||||
- The internal workings of checking out and returning books are hidden from the user, who interacts with simple methods (e.g., `check_out`, `return_book`).
|
||||
|
||||
#### Inheritance
|
||||
- If we have different types of books (e.g., EBook, Audiobook), we can create subclasses that inherit from the `Book` class.
|
||||
|
||||
```python
|
||||
class EBook(Book):
|
||||
def __init__(self, title, author, isbn, file_format):
|
||||
super().__init__(title, author, isbn)
|
||||
self.file_format = file_format
|
||||
|
||||
class Audiobook(Book):
|
||||
def __init__(self, title, author, isbn, duration):
|
||||
super().__init__(title, author, isbn)
|
||||
self.duration = duration
|
||||
```
|
||||
|
||||
#### Polymorphism
|
||||
- Different types of books can override methods if necessary. For example, an `Audiobook` might have a method to play the book, which other books don't have.
|
||||
|
||||
### Conclusion
|
||||
|
||||
Thinking in terms of objects involves identifying the key entities in your problem domain and modeling them as classes with attributes and methods. By encapsulating related data and behavior, abstracting complex logic, leveraging inheritance for code reuse, and utilizing polymorphism for flexibility, you can design robust and maintainable software systems.
|
||||
|
||||
---
|
||||
|
||||
### Key Core Concepts to Commit to Memory
|
||||
|
||||
When working with data structures in Python, certain concepts become second nature over time due to their fundamental importance and frequent use. Here’s a roundup of these key concepts:
|
||||
|
||||
Reference in New Issue
Block a user