import exampleĪll the names defined in our current namespace can be found out using the dir() function without any arguments. All other names that begin with an underscore are default Python attributes associated with the module (not user-defined).įor example, the _name_ attribute contains the name of the module. Here, we can see a sorted list of names (along with add). We can use dir in example module in the following way: dir(example) In Python, we can use the dir() function to list all the function names in a module.įor example, earlier we have defined a function add() in the module example. It also hampers the readability of our code. This can lead to duplicate definitions for an identifier. Importing everything with the asterisk (*) symbol is not a good programming practice. This includes all names visible in our scope except those beginning with an underscore(private definitions). Here, we have imported all the definitions from the math module. ![]() In Python, we can import all names(definitions) from a module using the following construct: # import all names from the standard module math Here, we imported only the pi attribute from the math module. For example, # import only pi from math module for Python3 this one: sudo apt-get install python3-rpi.gpio. We can import specific names from a module without importing the module as a whole. If Python RPi module is missing, one can install pip with all dependencies and then install the module using pip or go with the better approach and install the module alone: sudo apt-get install python-rpi.gpio. Hence, math.pi is invalid, and m.pi is the correct implementation. Note that the name math is not recognized in our scope. This can save us typing time in some cases. Here, We have renamed the math module as m. For example, # import module by renaming it In Python, we can also import a module by renaming it. For example, # import standard math module Suppose we want to get the value of pi, first we import the math module and use math.pi. For example: addition.add(4,5) # returns 9 Using the module name we can access the function using the dot. It only imports the module name example there. This does not import the names of the functions defined in example directly in the current symbol table. To import our previously defined module example, we type the following in the Python prompt. ![]() ![]() We can import the definitions inside a module to another module or the interactive interpreter in Python. The function takes in two numbers and returns their sum. Here, we have defined a function add() inside a module named example. Type the following and save it as example.py. A module may contain variables, functions, classes etc. Module is a file that contains code to perform a specific task. This makes our code organized and easier to maintain. Instead of putting everything in a single file, we can use modules to separate codes in separate files as per their functionality. As our program grows bigger, it may contain many lines of code.
0 Comments
Leave a Reply. |