Python Functions and Methods
Functions
A function is a block of code that is named (the name of the function), and which allows that block to be called elsewhere in a program by calling the name of the function.
Example
def myFunction(value): x = value do_work(x) myFunction(10)
This reduces the amount of code you need to repeat in your programs since your write the code in one place and just call it be name whereever you need to. It allows you to break up your code into logical, self contained blocks, making your code more readable. Functions also mean you can fix errors in one place rather than having to fix the error in every place that code was repeated.
Notice that the function is defined at the top level of the code, meaning it's not defined inside another object.
Methods
A method in Python is a function that's declared inside a class.
def MyClass: def myMethod(value): x = value do_work(x) object = MyClass() object.myMethod(10)
In Python, a method can be an instance method (the default) meaning it's called by an object. A class method is a method called by the class, not by an instance of the class, and has access to objects defined as part of the class. A static method is a method defined in a class, and referred to by the class in which its defined, but has no access to values defined within the class.
Example
# Let's declare a class, and in that class will be methods class myClass: classValue = "a value accessible by the class" def __init__(self): self.instanceValue = "A value accessible by an object instantiated as this class"; # An instance method def instanceMethod(self): # "self" refers to the instance of this Class print("Running instanceMethod.") print(f"instanceValue = {self.instanceValue}") print() # A class method @classmethod def classMethod(cls): # "cls" refers to the Class itself print("Running classMethod.") print(f"classValue = {cls.classValue} (But we can't access self.instanceValue)") print() # A static method @staticmethod def staticMethod(): print("Running staticMethod.") print("(We can''t access self.* or cls.* objects here)") # and now we call the functions object = myClass() object.instanceMethod() # call the instance method using the object myClass.classMethod() # call the class method using the class myClass.staticMethod() # call the static method using the class
Output
Running instanceMethod. instanceValue = A value accessible by an object instantiated as this class Running classMethod. classValue = a value accessible by the class (But we cant access self.instanceValue) Running staticMethod. (We cant access self.* or cls.* objects here)
To call an instance method we create an object of the class type and use it to call the method. To call class and static methods we use the class directly to call the method.
The difference between functions and methods in Python
Methods | Functions |
---|---|
Methods are always defined in a class | Functions are defined outside of a class |
Methods are only callable by a class instance or by the class itself | Functions are called independently of a class or object |
Methods have access to an object's properties if called by the object | Functions only access to global variables or data passed in as parameters |
Why define methods inside a class instead of just using functions?
Defining methods in a class allows you to group similar methods together, but also provides the means to create a class that is self-contained. Code that uses a class shouldn't know how the class does its magic: it should only know what the class can do. Defining methods inside a class allows you to keep the implementation details wrapped up and out of the way.
Notes
Functions can take parameters and return values. Returning a value uses the keyword
return
.
def add(a, b): sum = a + b return sum mysum = add(1, 2) print(mysum)
Output
3
Functions cannot be empty, but sometimes you want to create a placeholder for a function. To do this, use the special keyword pass.
Example
def implementLater(): pass