Python 3.7: Calling Methods

Welcome back to our Python tutorial series.Today were going to talk about something that might be new for some of you calling methods in Python 3.7. This feature has been around since version 2.2, but it’s worth reviewing because it can save you a lot of time and effort when working with objects.
In Python, an object is essentially anything that has properties or actions associated with it. For example, if we have a car object, its properties might include the make, model, year, and color. Its actions might include accelerating, braking, turning left or right, etc. To access these properties or perform these actions, you call methods on the object using dot notation (`object_name.method_name()`) or parentheses (`object_name(args)`).
Let’s take a look at an example:

# Defining a class called Car
class Car:
    # Defining a constructor method that takes in two parameters, make and model
    def __init__(self, make, model):
        # Setting the make property of the Car object to the value of the make parameter
        self.make = make
        # Setting the model property of the Car object to the value of the model parameter
        self.model = model
        
    # Defining a method called accelerate that takes in the Car object as a parameter
    def accelerate(self):
        # Printing a message to indicate that the car is accelerating
        print("Accelerating...")
    
    # Defining a method called brake that takes in the Car object as a parameter
    def brake(self):
        # Printing a message to indicate that the car is braking
        print("Braking...")

# Creating an instance of the Car class and passing in the make and model parameters
my_car = Car("Toyota", "Camry")

# Calling the accelerate method on the my_car object using dot notation
my_car.accelerate()

# Calling the brake method on the my_car object using dot notation
my_car.brake()

# Output:
# Accelerating...
# Braking...

# Explanation:
# - The class keyword is used to define a new class called Car.
# - The __init__ method is a constructor that is called when a new instance of the Car class is created.
# - The self parameter refers to the current instance of the class and is used to access its properties and methods.
# - The make and model parameters are used to initialize the make and model properties of the Car object.
# - The accelerate and brake methods are defined to perform specific actions on the Car object.
# - The print() function is used to output a message to the console.
# - An instance of the Car class is created and assigned to the variable my_car.
# - The dot notation is used to access the methods of the my_car object and call them.
# - The output shows that the methods were successfully called and executed.

In this example, we’ve defined a `Car` class with two properties (make and model) and three methods (`__init__`, accelerate, and brake). The `__init__` method is called when the object is created to initialize its properties. The other two methods are actions that can be performed on the object using dot notation or parentheses:

# Define a class called Car
class Car:
    # Initialize the class with two properties: make and model
    def __init__(self, make, model):
        self.make = make
        self.model = model
    
    # Method to accelerate the car
    def accelerate(self):
        print("Accelerating...")
    
    # Method to brake the car
    def brake(self):
        print("Braking...")

# Create an instance of the Car class with make "Toyota" and model "Camry"
my_car = Car("Toyota", "Camry")

# Print the make of the car using dot notation
print(my_car.make)  # prints "Toyota"

# Call the accelerate method on the car using parentheses
my_car.accelerate()   # prints "Accelerating..."

# Call the brake method on the car using dot notation
my_car.brake()   # prints "Braking..."

In this example, we’ve created a new `Car` object called `my_car`. We then print out its make property using dot notation (`my_car.make`) and call the accelerate method on it using dot notation as well (`my_car.accelerate()`).
You can also pass arguments to methods when you call them:

# Creating a Car class with two properties, make and model
class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model
        
    # Defining a method to accelerate the car to a given speed
    def accelerate(self, speed):
        print("Accelerating to {} mph...".format(speed))
    
    # Defining a method to brake the car
    def brake(self):
        print("Braking...")

# Creating a new Car object called my_car with make "Toyota" and model "Camry"
my_car = Car("Toyota", "Camry")

# Printing out the make property of my_car using dot notation
print(my_car.make) # Output: Toyota

# Calling the accelerate method on my_car with a speed argument of 60 mph
my_car.accelerate(60) # Output: Accelerating to 60 mph...

# Calling the brake method on my_car
my_car.brake() # Output: Braking...

In this example, we’ve modified the `accelerate` method to accept a single argument called `speed`. When you call this method on an object, you can pass in a value for speed:

# Creating a Car class with two attributes: make and model
class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model

    # Defining the accelerate method with a single argument called speed
    def accelerate(self, speed):
        # Using f-strings to print a message with the given speed
        print(f"Accelerating to {speed} mph...")

# Creating an instance of the Car class with make "Toyota" and model "Camry"
my_car = Car("Toyota", "Camry")

# Calling the accelerate method on the my_car object and passing in a value of 60 for speed
my_car.accelerate(60)   # prints "Accelerating to 60 mph..."

# Output:
# Accelerating to 60 mph...

# The script creates a Car object with make "Toyota" and model "Camry" and calls the accelerate method on it with a speed of 60. The accelerate method uses f-strings to print a message with the given speed.

And that’s it! You now know how to call methods on objects in Python 3.7. Its a powerful feature that can save you time and effort when working with complex data structures or systems. Give it a try !

SICORPS