What is __all__ in Python?

March 23, 2022

What is __all__ in Python?

In this article we will learn what __all__ is in Python and how we should use this instruction in our software to take advantage of its benefits.

__all__ in Python thumb

What’s up programmer, ok? Let’s learn more about Python!

__all__ instruction is used when we create a module and we are going to export its properties and methods

Then __all__ takes action, with a list of strings that will be exactly the elements that we are going to export from this module

And they will be publicly accessible by the code that imported them, let’s see it in practice:

__all__ = ['a','b']

a = 5

b = 'test'

def c():

This is our module, which with __all__ will only export the variables a and b, that is, we cannot use the c function outside of it

Now look at our second file:

from module import *


Here we import everything from our module, but we can only use what is in __all__

That is, a and b, and if we try to use c, we will get an error

Check the output:


So that’s what __all__ is for, to make declarative what we’re going to export from our module, to be used publicly by others who import it

It is important to mention two things:

If __all__ is removed, everything will be exported normally;

__all__ only works for import *, direct import can access all methods normally;


In this article we learned what __all__ is for in Python

Basically, we can, through this instruction, control what is exported from our modules

But __all__ will only work through import *, direct import will ignore this instruction

Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x