Author: Jiang Xia

| zhihu: www.zhihu.com/people/1024…

| GitHub:github.com/JiangXia-10…

| CSDN:blog.csdn.net/qq_4115394…

| the nuggets: juejin. Cn/user / 651387…

| public no. : 1024 notes

This article contains 869 words and takes 5 minutes to read

There are many powerful functions built into Python that can be used for many purposes, but sometimes you need to define some functions and call them in other places. Sometimes you need to import many functions from third-party libraries and then call the methods in them. For example, if you need to reference OpencV and its functions, you can use the following methods:

import cv2
Copy the code

Then you can call some of the functions inside, such as the camera function:

cap = cv2.VideoCapture(0)
Copy the code

So how do you call some of the functions you define? In fact, like reference third-party library functions, you need to introduce a custom function file, and then directly call. For example, define a function and save it as python1.py, as follows

#python1 def say(): print(" python1 ")Copy the code

You can then reference it directly by calling say() in the python2.py file

Import python1 # file name Function: Call python1.say()Copy the code

Run the py file and the result is as follows:

Above is a custom function and the function file is in the same folder, if not in the same folder, cannot install the above way call, need to introduce a custom function file path, again, in another folder, for example, a custom python3. Py files, then the inside of the reference method in python2

#python3 def say(): print(" python3 def say ")Copy the code
The sys module provides many functions and variables to handle different parts of the Python runtime environment. Import sys # Add the path of the custom function files to the folder location. Append (r"E:\python\workspace\pythonDemo1\ crawler ") # reintroduce the file and call the function import python3 python3.say()Copy the code

Run the py file with the result:

The file path structure is as follows:

That’s a simple use of how to call custom functions in Python!

Finally, welcome to pay attention to the public number: 1024 notes, free access to massive learning resources (including video, source code, documents)!

Other recommendations:

  • Getting started with Python (3) : Using tuples
  • Getting started with Python (4) : Using sets
  • Getting Started with Python (5) : Dict usage
  • Getting Started with Python 6: Calling custom functions
  • Getting started with Python (I) : String formatting
  • Python crawls taobao commodity information and generates Excel