Introduction to Callables
The course is part of this learning path
This course introduces callables and is part of a series of content designed to help you learn to program with Python.
- Describe the concept of callable
- List two types of callable
- Describe how to call a callable
This course is designed for first-time developers who want to learn Python. Existing developers may want to skip.
This introductory course does not require prior programming knowledge.
Hello, and welcome! My name is Ben Lambert, and I’ll be your instructor for this course. This course is part of a series of content designed to help you learn to program with the Python programming language. Should you wish to ask me a specific question, you can do that with the contact details on screen. You can also reach support by using the email address: email@example.com. And one of our cloud experts will reply.
The Python runtime is built on the notion of objects. We know that objects can store data. For example, string objects can store the text “hello, world!” And an integer object can store numbers such as 42. Recall that objects model aspects of a concept using attributes. And they model behaviors using methods. Methods are able to run code when they’re called. The notion of calling code is an important concept to understand. So, in this lesson we’re going to dive into the concept that Python refers to as callables.
Callables are named pieces of code that will run when called. Where conditional statements are used for making decisions, callables are used for taking action. It’s worth taking a mental note here. The concept of a callable is modeled after this basic structure. They accept (optional) input. Then they perform some action. Then they return the results. This is the basic anatomy of a callable: input, action, output.
In order to obtain output from a callable, it must first be called and provided with any required input. Picture each callable as its own delivery service. You call the service and they ask you to provide some resources. Those resources are the input. Once they have the input they can use that to produce the final output. Then once the output is ready they deliver it to you. The Python language has multiple syntax rules for creating callables which are useful in different contexts. Each style for creating a callable-object differs depending on how the callable is used.
Though, regardless of how the callable is defined in-code, conceptually they’re all the same. Callables accept (optional) input. Then they perform some action. Then they return the results. An object’s methods are an example of a callable. When a method is called the code inside the method runs and then returns a value.
In this example, we’ve bound the name greeting to the string ‘hello, world!’ Notice it’s all lowercase. Let’s make it - title case by calling the title method. Don’t worry about the syntax for now, we’ll cover that later. This is an example of calling the title method. In this case, we’ve called it without providing any input because none is required.
Now let’s use the replace method. This method accepts two pieces of input. The first input is the sequence to find, the second input is the sequence to replace with. Notice this results in the string ‘hello, universe!’ A method is a specific type of callable which is linked to an object. Being linked to an object gives them the ability to interact with the object’s attributes.
In this example the replace method is linked to the string object which we’ve bound to the name greeting. This replace method models the behavior of being able to find and replace from a sequence. Methods are a special callable because they can accept input, though they can also access the attributes of the linked object. This enables methods to edit their own state. For example, a counter could update the count every time a method is called.
This is highly useful when modeling specific concepts. However, sometimes you’ll need to model actions which are self-contained. By that I mean that the input to the callable is all you need. You don’t need access to any attributes.
Here’s an example. Imagine that you want to add three numbers together. The required input for this consists of three input values. Which means everything that you need for this can be provided as input. This code doesn’t require access to any additional attributes. It’s more of a standalone unit. Python refers to these types of self contained callables as functions. Functions are a common means of modeling self contained actions. They allow developers to create individual units of code that serve a developer-defined purpose.
Because functions are callables, they can accept input, perform a task using the input, and then return the results. The syntax for creating a new function begins with the def keyword; followed by the name of the function. The name defines the purpose of the function. After the function name, we need to define the inputs. We need to take a quick trip down a rabbit hole. Callables are able to specify input. When talking about the code that defines a callable we call these input: parameters.
As in: this function specifies 3 parameters. When talking about actually calling a callable we refer to the input as arguments. As in: here are my 3 arguments for this function. Much of the time, it doesn’t really matter if you mix these up, or just use one or the other. However, know that there is a distinction between the two. A callable’s input parameters are enclosed in parentheses.
All callables can define multiple input parameters. Which implies that functions can accept multiple parameters. Parameters are just name bindings to objects which will be provided as arguments when the callable is called. When we define a new callable we have to determine its input. Then, once we know what input we require we can name the parameters appropriately.
Parameter names are separated by a comma. To end this line we include a colon and a new line. The contents of this function are indented using the standard indentation. So our first line begins with 4 spaces followed by the code. When the interpreter reads this first line it determines that we’re trying to create a new function. It creates a new function object which can be called later in the code.
Once that object is created the interpreter binds the function’s name to that object. This is just like names bound to any other object such as the name pi being bound to the float 3.1415. Once a name is bound it can be used as a stand-in for the bound object. The code defined in the function is what gets run when the function is called. Whatever code is inside the function’s scope is considered part of the function.
Just as there’s a syntax for creating callables. There’s syntax for calling them too. The syntax for calling a callable closely mirrors the part of the function definition between def and the colon. When calling a function we first specify its name. The name is bound by the runtime when the interpreter reads the function definition. After the name we provide any arguments between the open and close parenthesis. If a function doesn’t require input then this will call the function. For functions with required parameters we need to specify the objects that we wish to use as input. These are separated by a comma.
When this is called it takes the objects we provided and runs the function’s code. Then the code returns an object representing the results. The return keyword is used to return an object from a callable. Python includes many different functions built into the standard library. For example: one built-in function reads files. Another can determine the length of a sequence. There’s even a function that can determine if an object is callable. If an object is callable, it can be called using this syntax.
Conditionals and callables are the two ingredients required for making decisions in code, and taking action. With just these two concepts we can accomplish a lot. We can break our code into discrete units based on its purpose; and we can call the code as needed based on if some condition is True or False. A large amount of development time will be spent with these concepts. So don’t worry too much if some of this didn’t quite stick, yet. They’ll make more sense when you begin to use them for yourself.
Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:
A callable is a concept based on the idea of input -> action -> output.
Python includes different syntax for creating callables, depending on their use case. Two types of callable are: methods and functions.
The syntax for calling a callable is to specify the name of the callable followed by a set of parentheses containing any required input.
Alright, that's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!
Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.