
PYTHON — Imported Objects Using Dot Notation in Python
Hardware is easy to protect: lock it in a room, chain it to a desk, or buy a spare. Software is harder to protect, but it is also harder to steal: often it is easier to write it than to persuade someone to give it to you. — Richard Stallman
Imported Objects Using Dot Notation in Python
In Python, you can use dot notation to access objects that are imported from another module. This allows you to use functions, classes, or variables from an imported module in your current module by prefixing them with the name of the imported module followed by a dot. This tutorial will show you how to use dot notation for imported objects in Python.
To demonstrate how to use dot notation for imported objects, let’s consider an example where we have a module called “adder” with a function called “add” that adds two numbers together. We want to import this “adder” module into another module called “main” and use the “add” function from the “adder” module in the “main” module.
Here’s how you can do this in Python:
# adder.py
def add(x, y):
return x + y# main.py
import adder
result = adder.add(2, 2)
print(result)In this example, we import the “adder” module using the import statement. Then, we use dot notation to access the "add" function from the "adder" module in the "main" module by prefixing it with adder.. We can then call the "add" function with the appropriate arguments.
When you run the “main” module, it will output the result of adding 2 and 2, which is 4.
It’s important to note that when you import a module using the import statement, the entire namespace of the imported module is added to the namespace of the calling module under the name of the imported module (in this case, "adder"). This allows you to access all the objects (functions, classes, variables) defined in the imported module using dot notation.
In summary, dot notation allows you to access objects from an imported module in Python. By prefixing the name of the imported module followed by a dot, you can access functions, classes, and variables from the imported module in your current module.
I hope this tutorial helps you understand how to use dot notation for imported objects in Python!






