While learning Python functions, I came across several concepts that initially looked confusing. After practising them one by one, they became much easier to understand.
In this post, Iām sharing my notes and examples for:
- Positional-only arguments
- Keyword-only arguments
- Variable scope
- Local vs global variables
global- Nested functions
- Returning multiple values
- Functions as objects
1. Positional-Only Arguments /
Sometimes we want a function parameter to be passed only by position, not by its parameter name.
We can use / for this.
def student(name, age, /):
print(name, age)
This works:
student("Karthick", 25)
But this does not:
student(name="Karthick", age=25)
Everything before / is positional-only.
Example
def calculate(a, b, /):
return a + b
print(calculate(10, 20))
Output:
30
The / tells Python that a and b must be supplied positionally.
2. Keyword-Only Arguments *
We can also force parameters to be passed using their names.
For this, we use *.
def student(name, *, age):
print(name, age)
This works:
student("Karthick", age=25)
But this does not:
student("Karthick", 25)
Everything after * is keyword-only.
Easy way to remember
/ ā before / = positional-only
* ā after * = keyword-only
3. Variable Scope
Scope means where a variable can be accessed in a program.
For example:
def test():
x = 10
print(x)
test()
Here, x is available inside the function.
But:
print(x)
outside the function will cause an error because x belongs to the function's local scope.
4. Local vs Global Variables
Local Variable
A variable created inside a function is normally a local variable.
def test():
x = 100
print(x)
test()
Here, x is local to test().
Global Variable
A variable created outside a function is a global variable.
x = 50
def test():
print(x)
test()
Output:
50
The function can access the global variable.
Easy rule
Created inside a function ā Local variable
Created outside a function ā Global variable
5. The global Keyword
What if we want to modify a global variable from inside a function?
We can use global.
x = 10
def change():
global x
x = 20
change()
print(x)
Output:
20
Without global, this:
def change():
x = 20
would create a new local x instead of modifying the global one.
Important point
global x does not create a global variable.
It tells Python:
"Inside this function, I want to use the existing global
x."
6. Nested Functions
A function defined inside another function is called a nested function.
def outer():
def inner():
print("Hello")
inner()
outer()
Output:
Hello
Here:
outer()
ā
inner()
ā
Hello
The inner function can also access variables from the outer function.
def outer():
name = "Karthick"
def inner():
print(name)
inner()
outer()
Output:
Karthick
inner() doesn't have its own name, so it can access name from outer().
7. Functions Returning Multiple Values
A Python function can return multiple values.
def calculate(a, b):
return a + b, a * b, a - b
We can store the returned values separately:
sum_value, product, difference = calculate(5, 2)
print(sum_value)
print(product)
print(difference)
Output:
7
10
3
Python actually packs the returned values into a tuple:
(7, 10, 3)
Then we unpack them:
(7, 10, 3)
ā ā ā
sum product difference
Important rule
The number of variables should match the number of values being unpacked.
For example:
def test():
return 10, 20, 30
a, b, c = test()
This works.
But:
a, b = test()
causes an error because there are 3 returned values but only 2 variables.
8. Functions as Objects
One of the interesting things about Python is that functions are objects.
Consider:
def greet():
return "Hello"
We can assign the function to another variable:
x = greet
Notice that there are no parentheses.
Now x refers to the same function.
So we can do:
print(x())
Output:
Hello
Important difference
x = greet
means:
xrefers to the function itself.
Whereas:
x = greet()
means:
Call the function and store its returned value in
x.
For example:
def square(n):
return n * n
operation = square
result = operation(5)
print(result)
Output:
25
The flow is:
square
ā
function object
ā
operation = square
ā
operation(5)
ā
square(5)
ā
25
This is the foundation for passing functions to other functions.
I'm continuing my Python function learning with the next concept: passing functions to other functions.
Top comments (0)