python logo


Tag: oop

python class

encapsulation in python

Method overloading

method overloading Several ways to call a method (method overloading)

In Python you can define a method in such a way that there are multiple ways to call it.

Given a single method or function, we can specify the number of parameters ourself.

Depending on the function definition, it can be called with zero, one, two or more parameters.

This is known as method overloading. Not all programming languages support method overloading, but Python does.

Related course
Python Programming Bootcamp: Go from zero to hero

Method overloading example


We create a class with one method sayHello(). The first parameter of this method is set to None, this gives us the option to call it with or without a parameter.

An object is created based on the class, and we call its method using zero and one parameter.

#!/usr/bin/env python

class Human:

def sayHello(self, name=None):

if name is not None:
print('Hello ' + name)
else:
print('Hello ')


# Create instance
obj = Human()

# Call the method
obj.sayHello()

# Call the method with a parameter
obj.sayHello('Guido')

Output:

Hello
Hello Guido

To clarify method overloading, we can now call the method sayHello() in two ways:

obj.sayHello()
obj.sayHello('Guido')

We created a method that can be called with fewer arguments than it is defined to allow.

We are not limited to two variables, your method could have more variables which are optional.

If you are new to Python programming, I highly recommend this book.

Download Exercises

python class inheritance

python inner class

An inner class, also known as a nested class, is a class that’s defined within the scope of another class. When an object is instantiated from an outer class, the object inside the nested class can also be used. It’s possible for a class to contain multiple nested classes, though they’re often used sparingly.

Python’s ability to nest classes within other classes is one of its many versatile features. A nested class inherits all attributes and methods from its enclosing class.

Related Course: Python Programming Bootcamp: Go from zero to hero

Inner Class in Python: A Simple Example

In Python, inner classes or nested classes can be defined inside the scope of another class. These inner classes can access all the members of their enclosing class.

Proper indentation is crucial when defining inner classes. Typically, each inner class is indented by 4 spaces to adhere to PEP 8, Python’s style guide.

Below is an example where we define a Human class with a nested Head class. We then create an instance of the Human class and call a method from the nested Head class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python

class Human:
def __init__(self):
self.name = 'Guido'
self.head = self.Head()

class Head:
def talk(self):
return 'talking...'

if __name__ == '__main__':
guido = Human()
print(guido.name)
print(guido.head.talk())

Output:

1
2
Guido
talking...

In the example above, the inner Head class possesses its own method. Inner classes can encapsulate both methods and variables. The constructor of the Human class (__init__) initializes a new head object.

Multiple Inner Classes in Python

Python doesn’t impose a limitation on the number of inner classes. Here’s an example that includes two nested classes within the Human class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python

class Human:
def __init__(self):
self.name = 'Guido'
self.head = self.Head()
self.brain = self.Brain()

class Head:
def talk(self):
return 'talking...'

class Brain:
def think(self):
return 'thinking...'

if __name__ == '__main__':
guido = Human()
print(guido.name)
print(guido.head.talk())
print(guido.brain.think())

Utilizing inner classes allows for more organized and object-oriented code. A single outer object can comprise multiple sub-objects, enabling a more structured programming approach.

Benefits of Using Inner Classes

Grouping classes within other classes through the use of inner classes can offer several benefits:

  1. Inner classes are confined to a local scope, ensuring encapsulation.
  2. It becomes more evident which classes share a relationship, enhancing code readability.

Although inner or nested classes provide structural benefits, they’re not as commonly used in Python compared to other OOP languages.

Download Exercises