python中不同文件中函数和类的调用
最近在学习Python的时候,遇到了一个不同文件中类无法调用的问题,搜了很多,发现很多人针对
这个问题都说的相当含糊,让我费了好大劲才把这个东东搞明白。记录一下,权且温习。
调用分两种,一种是同种文件路径下的调用,这种一般的方法是:
比如,文件b.py 调用a.py中的函数testa():
方法一:
from a import *
testa()
方法二:
import a
a.testa()
栗子:
文件aQueue.py
class QueueA:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def enqueue(self, item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
文件afunc.py
def test_call_file(input1,input2):
print("file is afunc")
input1 = input2 + 1
print("inputa:%d inputb:%d"%(input1,input2))
文件b.py的调用:
from aQueue import *
from afunc import *
q = QueueA()
q.enqueue('hello')
q.enqueue('hello')
q.enqueue('hello')
q.enqueue('hello')
print q.size() test_call_file(2,3)
文件c.py的调用:
import afunc
import aQueue
q = aQueue.QueueA()
q.enqueue('hello')
q.enqueue('hello')
q.enqueue('hello')
q.enqueue('hello')
print q.size() afunc.test_call_file(2,3)
不同文件路径下的调用:
import sys
sys.path.append('adress_of_B') #adress_of_B 表示文件B的地址
import b
b.fun()
实例和上面差不多,只需要把目录换一下就行了。这里就不贴了。
python中不同文件中函数和类的调用的更多相关文章
- python操作txt文件中数据教程[4]-python去掉txt文件行尾换行
python操作txt文件中数据教程[4]-python去掉txt文件行尾换行 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文章 python操作txt文件中数据教程[1]-使用pyt ...
- python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...
- python操作txt文件中数据教程[2]-python提取txt文件
python操作txt文件中数据教程[2]-python提取txt文件中的行列元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果-将txt中元素提取并保存在c ...
- python操作txt文件中数据教程[1]-使用python读写txt文件
python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...
- 使用Python从PDF文件中提取数据
前言 数据是数据科学中任何分析的关键,大多数分析中最常用的数据集类型是存储在逗号分隔值(csv)表中的干净数据.然而,由于可移植文档格式(pdf)文件是最常用的文件格式之一,因此每个数据科学家都应该了 ...
- rpm 系 linux 系统中 repo 文件中的 $release 到底等于多少?
rpm 系 linux 系统中 repo 文件中的 $release 到底等于多少? 结论 对于 8 来说,通过以下命令 #/usr/libexec/platform-python -c 'impor ...
- 在java的多态调用中,new的是哪一个类就是调用的哪个类的方法。
在java的多态调用中,new的是哪一个类就是调用的哪个类的方法.(x) 原因: ava多态有两种情况:重载和覆写 在覆写中,运用的是动态单分配,是根据new的类型确定对象,从而确定调用的方法: 在重 ...
- tornado 模版继承 函数和类的调用
模版继承.函数和类的调用 目录结构 lesson5.py # -*- coding:utf-8 -*- import tornado.web import tornado.httpserver imp ...
- Python:如何删除文件中的空白行?
def delblankline(infile,outfile): infopen = open(infile,'r') outfopen = open(outfile,'w') lines = in ...
- Java笔记(二十七)……IO流中 File文件对象与Properties类
File类 用来将文件或目录封装成对象 方便对文件或目录信息进行处理 File对象可以作为参数传递给流进行操作 File类常用方法 创建 booleancreateNewFile():创建新文件,如果 ...
随机推荐
- ELK-WEB中文汉化和安全认证
1.Kibana汉化方法此项目,适用于Kibana 5.x-6.x的任意版本,汉化过程不可逆 1)Github仓库下载kibana中文汉化包,下载指令如下: git clone https://git ...
- echarts Cannot read property 'getAttribute' of null 问题的解决方法
今天在使用echarts练习官方给的实例的时候,代码没有问题 却总是报错“Cannot read property 'getAttribute' of null” 找了好久的文档没有看明白 ... 最 ...
- OpenStack 安装:glance 安装
接上一篇keystone, 这一篇介绍glance服务: 在开始操作之前,先用source环境变量,然后创建glance 用户,并设置密码为glance [root@linux-node1 ~]#op ...
- 十四、ChainOfResponsibility 责任链模式
设计: 代码清单: Trouble: public class Trouble { private int number; public Trouble(int number){ this.numbe ...
- dagScheduler
由一个action动作触发sparkcontext的runjob,再由此触发dagScheduler.runJob,然后触发submitJob,封装一个JobSubmitted放入一个队列.然后再通过 ...
- IPV4/IPV6双协议栈配置案例
拓扑: XRV1配置: =================================================================== hostname XRV1! ipv6 ...
- [网络流]Farm Tour(费用流
Farm Tour 题目描述 When FJ's friends visit him on the farm, he likes to show them around. His farm compr ...
- AJAX html 传输json字符串&&巧妙运用eval()来解析返回的JSON字符串
1.AJAX html 传输json字符串: js方法如下: function saveRetYwlsh(){ var xmbh = document.getElementById("xmb ...
- docker日志设置定期清理
1.新建/etc/docker/daemon.json,若有就不用新建了 2.添加log-dirver和log-opts参数,样例如下 "log-driver":"jso ...
- ----关于position的四个标签----
从[ two1-4 ]分别为absolute,fixed,static,relative标签 四个标签下位移值相同,[ two2 ]和[ two1 ]都出现在左上角,[ two2 ] 盖住了[ two ...