In python, you can reference the function of another file. Here is an example: First, the calling method in the same folder is as follows:
Call the function :
A.py: def add(x,y): print('和为:%d'%(x+y))
B.py file:
import A A.add(1,2) ###或者### from A import add add(1,2)
If not in a folder:
import sys sys.path.append(r'E:\PythonProject\winycg')
' ''When python imports the module, it is searched in sys.path in order.
sys.path is a list that stores many paths in the form of strings.
To use the function in the A.py file, you need to first put its file path in sys.path'''
import A a=A.A(2,3) a.add()
Sometimes, it will prompt ## during execution #No module named "xxx", referring to the __init__.py file for modification still cannot achieve the desired effect. Therefore, in this case, the following methods have been tried to achieve the goal.
When the .py file to be referenced is not in the same folder as the file currently being executed, we can still implement it directly using the third line of code below. You can see that the first two lines are commented out.#import sys #sys.path.append(r'D:\Python27\Pythonfiles\HMM') from Pythonfiles.HMM import M_matrix ##引用M矩阵The above M_matrix.py file converts the time series into a certain matrix form, which makes it easier for us to read different files and represent the M matrix into a file. At this time, the parameters can be called Refer to the following form:
def matrix(X): par = M_matrix.parameters(X) M = M_matrix.center(X, par[5], par[6], par[7], par[8], par[9]) # 获取转换后的M矩阵 return M
The above is the detailed content of Can python reference a function of another file?. For more information, please follow other related articles on the PHP Chinese website!

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
