Functions

General Declaration#

We generally declare functions as instance methods as such:

def functionDouble(x):
return 2 * x

Here x is the parameter. To call the function, for example, we supply 10 as the argument here: functionDouble(10).

Class / Static Methods#

Class Methods#

Class methods are shared among all instances and they are called with the calling class as the first argument.

@classmethod
def get_type(cls):
return cls.type

Static Methods#

Static methods are called without any class or instance reference.

@staticmethod
def get_type():
return 'type'

Optional Arguments#

Function parameters can have default values as shown. In the below example, data and next_node are optional arguments to be provided by the calling program.

def __init__(self, data=None, next_node=None):
self.data = data
self.next_node = next_node