Skip to content

Commit

Permalink
Create Methods-Attributes.md
Browse files Browse the repository at this point in the history
  • Loading branch information
0joseDark authored Nov 7, 2024
1 parent 9873868 commit bcfdaa5
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions English/Methods-Attributes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
In programming, especially in object-oriented programming (OOP), **methods** and **attributes** are foundational concepts that define the behavior and characteristics of objects in a class. Here's a breakdown of each:

### 1. **Attributes**

- **Definition**: Attributes are variables that hold data related to an object. They define the characteristics or properties of the object.
- **Example**: Imagine a `Car` class. Attributes for a car might include `color`, `brand`, `year`, etc.

#### Code Example

```python
class Car:
def __init__(self, color, brand, year):
self.color = color # Attribute
self.brand = brand # Attribute
self.year = year # Attribute

# Creating an object of Car class
my_car = Car("Red", "Toyota", 2020)

# Accessing attributes
print(my_car.color) # Output: Red
print(my_car.brand) # Output: Toyota
print(my_car.year) # Output: 2020
```

Here, `color`, `brand`, and `year` are attributes of the `Car` object. Each instance of `Car` can have different values for these attributes.

### 2. **Methods**

- **Definition**: Methods are functions defined within a class that perform actions using the object’s data or manipulate the object’s state. They define the behavior of an object.
- **Example**: In the `Car` class, methods might include `start()`, `stop()`, or `accelerate()` to perform specific actions.

#### Code Example

```python
class Car:
def __init__(self, color, brand, year):
self.color = color
self.brand = brand
self.year = year

def start(self): # Method
print(f"The {self.color} {self.brand} car is starting.")

def stop(self): # Method
print(f"The {self.color} {self.brand} car is stopping.")

# Creating an object of Car class
my_car = Car("Red", "Toyota", 2020)

# Calling methods
my_car.start() # Output: The Red Toyota car is starting.
my_car.stop() # Output: The Red Toyota car is stopping.
```

In this example, `start` and `stop` are methods that describe behaviors of the `Car` object.

### Key Differences

- **Attributes** represent **data** about the object, while **methods** represent **actions** or **behaviors** that the object can perform.
- Attributes are often variables, while methods are functions defined within the class.

### Summary Table

| **Concept** | **Purpose** | **Example** in Code |
|--------------|-------------------------------------|----------------------------------|
| **Attribute** | Holds data about the object | `self.color`, `self.year` |
| **Method** | Performs an action or behavior | `start()`, `stop()` |

By defining both attributes and methods, classes create a blueprint for objects that combine state and behavior, helping to model real-world entities in a program.

0 comments on commit bcfdaa5

Please sign in to comment.