Add tech_docs/python/python_classes.md
This commit is contained in:
129
tech_docs/python/python_classes.md
Normal file
129
tech_docs/python/python_classes.md
Normal file
@@ -0,0 +1,129 @@
|
||||
Sure, let's take a step back and go through the basics of classes and the use of `self` in Python, ensuring you have a full understanding.
|
||||
|
||||
### Classes in Python
|
||||
|
||||
A **class** is a blueprint for creating objects. Classes encapsulate data and functions that operate on that data. When you create an instance of a class, you create an object.
|
||||
|
||||
### Understanding `self`
|
||||
|
||||
The `self` parameter in Python is a reference to the current instance of the class. It allows you to access the attributes and methods of the class in the instance context. It's called `self` by convention, but you could name it anything (though using `self` is a widely followed convention and it's best to stick to it).
|
||||
|
||||
### Basic Structure of a Class
|
||||
|
||||
1. **Class Definition**: Define a class using the `class` keyword.
|
||||
2. **Constructor Method (`__init__`)**: This special method initializes the object's attributes.
|
||||
3. **Attributes**: Variables that belong to the object.
|
||||
4. **Methods**: Functions that belong to the object and operate on its attributes.
|
||||
|
||||
### Example
|
||||
|
||||
Let's create a simple class to understand these concepts better.
|
||||
|
||||
#### Step-by-Step Example
|
||||
|
||||
1. **Define a Class**:
|
||||
|
||||
```python
|
||||
class Page:
|
||||
def __init__(self, title, content):
|
||||
self.title = title
|
||||
self.content = content
|
||||
```
|
||||
|
||||
2. **Constructor Method (`__init__`)**:
|
||||
- The `__init__` method initializes the attributes of the class.
|
||||
- `self.title` and `self.content` are instance attributes initialized with the values provided when an object is created.
|
||||
|
||||
```python
|
||||
class Page:
|
||||
def __init__(self, title, content):
|
||||
self.title = title
|
||||
self.content = content
|
||||
```
|
||||
|
||||
3. **Create an Object**:
|
||||
- Create an instance of the class by calling the class with the required arguments.
|
||||
|
||||
```python
|
||||
home_page = Page("Home", "Welcome to the homepage!")
|
||||
```
|
||||
|
||||
4. **Access Attributes**:
|
||||
- Access the attributes of the object using the dot notation.
|
||||
|
||||
```python
|
||||
print(home_page.title) # Outputs: Home
|
||||
print(home_page.content) # Outputs: Welcome to the homepage!
|
||||
```
|
||||
|
||||
5. **Define Methods**:
|
||||
- Methods are functions that operate on the object's attributes.
|
||||
|
||||
```python
|
||||
class Page:
|
||||
def __init__(self, title, content):
|
||||
self.title = title
|
||||
self.content = content
|
||||
|
||||
def display(self):
|
||||
return f"Title: {self.title}\nContent: {self.content}"
|
||||
```
|
||||
|
||||
6. **Call Methods**:
|
||||
- Call the method on the object to perform actions or return values based on the object's attributes.
|
||||
|
||||
```python
|
||||
home_page = Page("Home", "Welcome to the homepage!")
|
||||
print(home_page.display()) # Outputs: Title: Home, Content: Welcome to the homepage!
|
||||
```
|
||||
|
||||
### Full Example
|
||||
|
||||
Here's the full example with comments explaining each part:
|
||||
|
||||
```python
|
||||
class Page:
|
||||
# Constructor method to initialize the attributes
|
||||
def __init__(self, title, content):
|
||||
self.title = title # Initialize the title attribute
|
||||
self.content = content # Initialize the content attribute
|
||||
|
||||
# Method to display the title and content
|
||||
def display(self):
|
||||
return f"Title: {self.title}\nContent: {self.content}"
|
||||
|
||||
# Create an instance of the Page class
|
||||
home_page = Page("Home", "Welcome to the homepage!")
|
||||
|
||||
# Access the attributes
|
||||
print(home_page.title) # Outputs: Home
|
||||
print(home_page.content) # Outputs: Welcome to the homepage!
|
||||
|
||||
# Call the display method
|
||||
print(home_page.display()) # Outputs: Title: Home, Content: Welcome to the homepage!
|
||||
```
|
||||
|
||||
### The `self` Joke
|
||||
|
||||
The joke about `self` often goes something like this:
|
||||
|
||||
```python
|
||||
class Foo:
|
||||
def bar(self):
|
||||
print("Hello, self!")
|
||||
|
||||
foo = Foo()
|
||||
foo.bar() # Outputs: Hello, self!
|
||||
```
|
||||
|
||||
This joke plays on the fact that `self` is the conventional name for the first parameter of instance methods, which refers to the instance calling the method. It highlights how `self` is used to access the attributes and methods of the class from within the class.
|
||||
|
||||
### Recap
|
||||
|
||||
- **Class**: A blueprint for creating objects.
|
||||
- **Object**: An instance of a class.
|
||||
- **Attributes**: Data stored inside an object.
|
||||
- **Methods**: Functions defined in a class that operate on the object's attributes.
|
||||
- **`self`**: A reference to the current instance of the class, used to access attributes and methods from within the class.
|
||||
|
||||
By understanding these concepts, you can effectively use classes to model real-world entities, encapsulate data, and define behaviors, making your code more modular, reusable, and easier to maintain.
|
||||
Reference in New Issue
Block a user