最近在学习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中不同文件中函数和类的调用的更多相关文章

  1. python操作txt文件中数据教程[4]-python去掉txt文件行尾换行

    python操作txt文件中数据教程[4]-python去掉txt文件行尾换行 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文章 python操作txt文件中数据教程[1]-使用pyt ...

  2. python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件

    python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...

  3. python操作txt文件中数据教程[2]-python提取txt文件

    python操作txt文件中数据教程[2]-python提取txt文件中的行列元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果-将txt中元素提取并保存在c ...

  4. python操作txt文件中数据教程[1]-使用python读写txt文件

    python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...

  5. 使用Python从PDF文件中提取数据

    前言 数据是数据科学中任何分析的关键,大多数分析中最常用的数据集类型是存储在逗号分隔值(csv)表中的干净数据.然而,由于可移植文档格式(pdf)文件是最常用的文件格式之一,因此每个数据科学家都应该了 ...

  6. rpm 系 linux 系统中 repo 文件中的 $release 到底等于多少?

    rpm 系 linux 系统中 repo 文件中的 $release 到底等于多少? 结论 对于 8 来说,通过以下命令 #/usr/libexec/platform-python -c 'impor ...

  7. 在java的多态调用中,new的是哪一个类就是调用的哪个类的方法。

    在java的多态调用中,new的是哪一个类就是调用的哪个类的方法.(x) 原因: ava多态有两种情况:重载和覆写 在覆写中,运用的是动态单分配,是根据new的类型确定对象,从而确定调用的方法: 在重 ...

  8. tornado 模版继承 函数和类的调用

    模版继承.函数和类的调用 目录结构 lesson5.py # -*- coding:utf-8 -*- import tornado.web import tornado.httpserver imp ...

  9. Python:如何删除文件中的空白行?

    def delblankline(infile,outfile): infopen = open(infile,'r') outfopen = open(outfile,'w') lines = in ...

  10. Java笔记(二十七)……IO流中 File文件对象与Properties类

    File类 用来将文件或目录封装成对象 方便对文件或目录信息进行处理 File对象可以作为参数传递给流进行操作 File类常用方法 创建 booleancreateNewFile():创建新文件,如果 ...

随机推荐

  1. uboot1.1.6

    http://blog.csdn.net/lizuobin2/article/details/52061530

  2. web.xml hello1代码分析

    在“Web页”节点下,展开WEB-INF节点,然后双击web.xml文件进行查看. 上下文参数提供Web应用程序所需的配置信息.应用程序可以定义自己的上下文参数.此外,JavaServer Faces ...

  3. Modelsim command line 传参数到 .do 文件

    gui跑mdelsim总觉得很麻烦,使用命令来启动方便了很多,类似linux一样,其实目前windows也可以做到,只是业界不怎么用windows罢了. 基于modelsim搭了一个UVM环境,  用 ...

  4. 解决Windows 10 1803 April 2018 Updatete不能网络共享的问题

    Windows 10升级到1803后便不能网络共享了,现在我用的是Widnows 10 1809 Oct 2018 Update依然存在这个问题. 为了能够共享文件和文件夹需要去windows ser ...

  5. redis 动态修改配置与备份文件目录

    redis-cli -c -h 10.1.1.1 -p 7000 获取所有可以配置的KEY config get * 设置KEY config set * 配置重新写入配置文件 CONFIG REWR ...

  6. as3.0画直线

    import flash.display.Shape; import flash.events.MouseEvent; import flash.geom.Point; var line:Shape; ...

  7. matplotlib坐标轴的一些操作

  8. TPL DataFlow初探(一)

    属性TPL Dataflow是微软面向高并发应用而推出的一个类库.借助于异步消息传递与管道,它可以提供比线程池更好的控制,也比手工线程方式具备更好的性能.我们常常可以消息传递,生产-消费模式或Acto ...

  9. hmtl div水平、垂直居中

    最近写网页经常需要将div在屏幕中居中显示,遂记录下几个常用的方法,都比较简单.水平居中直接加上<center>标签即可,或者设置margin:auto;当然也可以用下面的方法 下面说两种 ...

  10. 394. Decode String 解码icc字符串3[i2[c]]

    [抄题]: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], ...