python inner class
Python hosting: Host, run, and code Python in the cloud!
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 | #!/usr/bin/env python |
Output:
1 | Guido |
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 | #!/usr/bin/env python |
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:
- Inner classes are confined to a local scope, ensuring encapsulation.
- 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.
Leave a Reply:
Hi Frank! I just wonder why __name__ here set to __main__?
I can figure the guido.name head.talk(), brain.think(), and the other parts and how it works.
But I'm totally confuse, about __main__ condition.
Is it because __name__ is private variable?
What if I replace __main__ here with __main, like your example variable structure in encapsulation?
An answer will be valuable to me.
Ps. I'm just totally beginner, but your tutorial are the easiest one to be understood.
Thank's
Hi! Thanks! Python has an execution entry point called main. When you execute a Python script, it is used as the main function (starting point of the program) if the __name__ attribute is set to "__main__".
If you import this script as a module, the __name__ is set to the name of the script/module and it will not be executed.
The code below is used to execute some code only if the file was run directly, and not imported.
Let us do an example, we create a python file called file.py:
And file2.py:
If we run either of these two scripts, it will execute only the code in __main__. Thus the output is either "Hello from file.py" or "Hello from file2.py".
However, if we change file.py to:
and run file2.py, it will execute all code in file.py and in the __main__ of file2.py, thus outputting:
In short, we need __main__ if we want to couple python files together. Without __main__, Python will execute all code from every file that is imported.
If you change to __main it will not execute or use it as starting point of a script because __main__ is a special variable in Python.
That means that you do not have to use __main__ at all if you are not importing the script anywhere, meaning this will work too:
In your (files).py examples, if the __main__ code in file.py are removed, it will make file.py become regular class, and file2.py become the main class, like in Java?
If that so, in my mind, simple Java program always have main class (public static void main) that will execute the listed program, while in python some simple program didn't always need __main__ to execute the listed program.
Am I correct?
Sorry, completely new to python world, and totally forgot java environment after years.
Thank's for your patience, Frank!
Yes, if __main__ is removed, file.py will become a regular class. Python will execute anything that is on the 0th level of indention from file.py, which is different form Java. If file.py would contain a class with methods, it can be included without any problems. The code below demonstrates:
file.py
file2.py
In usage its similar to Java's public static void main, with the exception that Python will execute any statement on the 0th level of indention.
Hi Frank,
I was wondering how would we show a content of a variable defined in the parent class in the inner class?
As far as i know, inner classes have no way of accessing outer class variables.