Python Methods and Calling Conventions

Today we’re going to talk about one of the most important concepts in Python programming methods and calling conventions. Now, if you’ve been coding for a while, this might seem like old hat, but bear with me here because I’m gonna put my own spin on it.

First things first: what are methods? Well, they’re basically functions that belong to an object or class. They allow us to perform operations on the data stored in those objects and classes without having to manipulate them directly. For example, let’s say we have a list of numbers called `my_list`. If we want to find out how many elements are in it, we can use the built-in method `len()` like so:

# Creating a list of numbers
my_list = [1, 2, 3]

# Using the built-in method len() to find the length of the list
# len() returns the number of elements in a list
length = len(my_list)

# Printing the result
print(length)

# Output: 3

Pretty straightforward, right? But what about when we want to do something a little more complex, like sorting the list in reverse order? Well, that’s where calling conventions come into play.In Python, there are two main ways to call methods: using dot notation or parentheses. Let’s take a look at both of them.

Dot Notation: This is probably the most common way to call methods in Python. It involves using a period (`.`) followed by the method name and any necessary arguments inside the parentheses. For example, let’s say we have an object called `my_obj` that has a method called `do_something()`. If we want to call this method with no arguments, we can do it like so:

# This script demonstrates how to call a method on an object in Python

# First, we create an object called `my_obj` using the `MyClass` class
my_obj = MyClass()

# Then, we call the `do_something()` method on `my_obj` using the dot notation
my_obj.do_something() # This line calls the `do_something()` method on `my_obj`

# The `do_something()` method does not take any arguments, so we do not need to pass any in the parentheses


If the method takes one or more arguments, you simply pass them in after the parentheses, separated by commas. For example:

# The following script is used to demonstrate how to pass arguments to a method in Python.

# First, we define a class called "MyClass" with a method called "do_something".
class MyClass:
    def do_something(self, arg1, arg2): # The "self" parameter is used to refer to the current instance of the class.
        print(arg1 + arg2) # This method simply prints the sum of the two arguments passed to it.

# Next, we create an instance of the class and assign it to the variable "my_obj".
my_obj = MyClass()

# Finally, we call the "do_something" method on our instance and pass in two arguments, "arg1" and "arg2".
my_obj.do_something(1, 2) # This will print the sum of 1 and 2, which is 3.

# Output: 3

Parentheses Notation: This is a less common way to call methods in Python, but it can be useful in certain situations. It involves using parentheses followed by the object name and any necessary arguments inside them. For example, let’s say we have an object called `my_obj` that has a method called `do_something()`. If we want to call this method with no arguments, we can do it like so:

# The following script demonstrates how to call a method on an object using parentheses and arguments.

# First, we define an object called `my_obj` with a method called `do_something()`.
my_obj = Object()

# To call the method with no arguments, we use parentheses after the object name.
my_obj.do_something()

# This will execute the code within the `do_something()` method and return the result.

# Note: Parentheses are used to indicate a function or method call in Python. They can also be used to pass arguments to the function or method.

If the method takes one or more arguments, you simply pass them in after the parentheses and object name, separated by commas. For example:

# The following script is used to call the do_something() method on an object, passing in two arguments.

# First, we define the object "my_obj" and assign it a value.
my_obj = "Hello"

# Next, we call the do_something() method on the object "my_obj", passing in two arguments "arg1" and "arg2".
my_obj.do_something(arg1, arg2)


# First, we define the object "my_obj" and assign it a value of "Hello".
my_obj = "Hello"

# Next, we call the do_something() method on the object "my_obj", passing in two arguments "arg1" and "arg2".

my_obj.do_something(arg1, arg2)

Now, I know what some of you might be thinking “Why would anyone use that second notation? It looks ridiculous!” And to that, I say…you’re right! But sometimes it can come in handy when dealing with complex expressions or chaining multiple method calls together. For example:

# Using a generator expression to call the method "do_something" on the object "my_obj" with the argument "arg1" for 5 times
generator = (my_obj.do_something(arg1) for i in range(5))

# Accessing the third element of the resulting generator object
result = generator[2]

# Printing the result
print(result)

This creates a generator expression that returns the third result of calling `do_something()` with argument `arg1`, and then iterating over it using a list comprehension. It’s not exactly elegant, but sometimes you gotta do what you gotta do!

Pretty simple stuff, right? Well…not really. But that’s the beauty of programming it’s always full of surprises!

SICORPS