zdj8023

转自: https://blog.csdn.net/winycg/article/details/78512300

 

在同一个文件夹下
调用函数:
A.py文件:

def add(x,y):
print(\'和为:%d\'%(x+y))

B.py文件:
import A
A.add(1,2)


from A import add
add(1,2)

调用类:
A.py文件:

class A:
def __init__(self,xx,yy):
self.x=xx
self.y=yy
def add(self):
print("x和y的和为:%d"%(self.x+self.y))


B.py文件:
from A import A
a=A(2,3)
a.add()


import A
a=A.A(2,3)
a.add()


在不同文件夹下
A.py文件的文件路径:E:\PythonProject\winycg

B.py文件:
import sys
sys.path.append(r\'E:\PythonProject\winycg\')
\'\'\'python import模块时, 是在sys.path里按顺序查找的。
sys.path是一个列表,里面以字符串的形式存储了许多路径。
使用A.py文件中的函数需要先将他的文件路径放到sys.path中\'\'\'
import A

a=A.A(2,3)
a.add()



分类:

技术点:

相关文章:

  • 2021-05-13
  • 2021-12-23
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2021-06-28
  • 2022-12-23
  • 2021-11-24
猜你喜欢
  • 2022-12-23
  • 2021-10-30
  • 2022-02-09
  • 2021-12-11
  • 2021-09-19
  • 2021-12-09
相关资源
相似解决方案