python調用其他文件中的函數或者類


01 在同一個文件夾下,調用函數或者類

A.py文件中

def test():
	print('this is test func')

class A:
    def test():
        print('this is a class named A, its func is test() ')
    

B.py文件中

# way 1
from A import test
from A import A

test()

a = A()
a.test()


# way 2
import A
A.test()
a = A.A()
a.test()
02 在不同文件夾下,調用函數或者類

src文件夾與B.py文件在同一目錄下,src文件夾下有C.py文件

C.py文件中

def test():
	print('this is test func')

class C:
    def test(self):
        print('this is a class named C, its func is test() ')
        

B.py文件中

from src.C import test
from src.C import C

test()

a = C()
a.test()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM