SoFunction
Updated on 2024-11-07

Calls to different modules (functions, classes, variables) in python explained in detail

First, let's start with two ways to introduce modules.

Method 1: Introducing the entire document

import filename

Filename. FunctionName( ) / filename. Class Name

With this method you can run a function from another file

Method 2: Introduce only one class/function/variable in a file

When you need to introduce more than one function or variable from a file, just separate them with commas

from file name import function name, class name, variable name

Next, a concrete example of how to introduce a module is shown:

Suppose a new python package test2 is created with a python file named and a function named running() in the file. Of course a __init__.py file was automatically generated when the test2 package was created. Now we need to run the running() function in a .py file outside the package.

First, the first step is that you need to introduce this module in a .py file outside of the package, and four ways to do that will be described here.

1. Introduce the run module first

from test2 import run

Calling the running() function

()

2. Directly introduce the run function in the run module, and then directly run this function

from  import running
running()

3. You need to introduce the running function in the __init__.py file in the test2 package.

# Introduce the running() function from the run module.
#. means to bring in from the current directory . is the parent directory
from .run import running

Then introduce the test2 package directly and use the package name. Function name, you can use the

import test2
()

4. Same as three. First you need to introduce the running function in the __init__.py file in the test2 package.

from .run import running

Then just bring in the running function directly

from test2 import running
running()

When introducing a very long function, rename the introduced function/class/variable with as

Example:

from test2 import sleep_time_from_time_or_day as e
e()

Above this on python in different modules (functions, classes, variables) calls in detail is all I have shared with you, I hope to give you a reference, and I hope you support me more.