The Python json.__all__ attribute is a list that defines the public API of the json module, specifying the names that are exported when from json import * is used.
This attribute helps in controlling what gets imported into the namespace when performing a wildcard import.
SyntaxFollowing is the syntax for accessing the json.__all__ attribute −
import json # Access the __all__ attribute print(json.__all__)Contents of json.__all__
The json.__all__ attribute includes the following key components of the json module −
In this example, we access the json.__all__ attribute to see its contents −
import json # Print the contents of json.__all__ print("Public API of json module:", json.__all__)
Following is the output obtained −
Public API of json module: ['dump', 'dumps', 'load', 'loads', 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder']Example: Using from json import *
Using from json import * imports only the names listed in json.__all__ −
from json import * # Using dumps function from json module json_string = dumps({"name": "Alice", "age": 25}) print("Serialized JSON:", json_string)
Following is the output of the above code −
Serialized JSON: {"name": "Alice", "age": 25}Example: Checking if a Name is in json.__all__
We can check if a function or class is part of the public API −
import json # Check if 'JSONEncoder' is in json.__all__ print("Is 'JSONEncoder' in json.__all__?", 'JSONEncoder' in json.__all__)
We get the output as shown below −
Is 'JSONEncoder' in json.__all__? TrueExample: Checking if a Function is in json.__all__
The json.__all__ attribute contains a list of publicly available functions in the json module. You can check if a specific function exists within this attribute using the in keyword −
import json # Check if 'dumps' is available in json.__all__ if "dumps" in json.__all__: print("dumps function is available in json module.") # Check if 'loads' is available in json.__all__ if "loads" in json.__all__: print("loads function is available in json module.")
The result produced is as follows −
dumps function is available in json module. loads function is available in json module.
python_json.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4