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():创建新文件,如果 ...
随机推荐
- 从零开始搭建自己的VueJS2.0+ElementUI单页面网站(一、环境搭建)
原网址:https://blog.csdn.net/u012907049/article/details/72764151 前言 VueJS可以说是近些年来最火的前端框架之一,越来越多的网站开始使用v ...
- 微信小程序自制提示框(具有输入文本功能)
https://blog.csdn.net/qq_41681675/article/details/81005561
- extern和include的作用
首先要搞清楚的是.h头文件中都是一些声明性的语句,是不分配内存的,所以头文件中有对函数的声明,有define语句,有没有实例化的结构体定义,但是没有对变量的定义(比如 int a),有的只是对外变量的 ...
- 基于python的selenium自动化测试环境搭建
Windows下的环境搭建: 1.安装python2.7.152.cmd里敲pip install selenium3.安装firefox47.geckodriver11(并将geckodriver. ...
- Linux驱动之同步、互斥、阻塞的应用
同步.互斥.阻塞的概念: 同步:在并发程序设计中,各进程对公共变量的访问必须加以制约,这种制约称为同步. 互斥机制:访问共享资源的代码区叫做临界区,这里的共享资源可能被多个线程需要,但这些共享资源又不 ...
- python小结 1
1.变量 记录状态 类型:数字,字符串,元组,列表,字典 可变不可变(内存地址不变的情况下,值能不能改变): 不可变:字符串,数字,元组 可变:列表,字典 访问顺序: 直接访问:数字 有序:字符串,列 ...
- Subarray Sums Divisible by K LT974
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- Minimum Domino Rotations For Equal Row LT1007
In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino. (A domi ...
- 第一次spring会议
1.今天查询了很多案例,找到了符合我们要求的案例,并进行了尝试. 2.昨天拍摄了宣传视频. 3.明天准备用C#限定格式输出TXT文件.
- python模块:datetime
# Stubs for datetime # NOTE: These are incomplete! import sys from typing import Optional, SupportsA ...