我需要从python中的另一个程序导入一个类

时间:2019-05-08 14:15:19

标签: python class

class memory:
# This calss is the memory reading and indexing part 
def __init__(self, memsave, memrestore, x, x1):
    self.x = x
    self.x1 = x1
    self.memsave = memsave
    self.memrestore = memrestore
    x = []
    x1 = []

def getx(self, QAtoget):
    filepathx1 = question_memory_value
    # The try checks to see if the Question Exists
    try:
        open(filepathx1)
    except ValueError:
        open(filepathx1, "w+")
    x = []
    # This opens the Question file to get all of the saved values
    with open(filepathx1) as fp:
        linex1 = fp.readline()
        count = 1
        while linex1:
            x.append(linex1.strip())
            linex1 = fp.readline()
            count += 1

    QAtoget = ">>>" + QAtoget
    print(QAtoget)
    print(x)
    if QAtoget in x:
        #This is the indexing part `itemplacement` is the varable that shows what entry the question is on
        itemplacement = x.index(QAtoget)
        return itemplacement
    else:
        return False
def findvalue(self, numlist):
    x1 = []
    #This is the file read in part where we see the memory that is stored
    filepathx2 = answer_memory_value
    with open(filepathx2) as fp:
        linex2 = fp.readline()
        count = 1
        while linex2:
            x1.append(linex2.strip())
            linex2 = fp.readline()
            count += 1
    #This is condesing the >>>QUESTION to just QUESTION
    returningvaluecondence = x1[numlist]
    memoryvaluecondeser = returningvaluecondence[3:len(returningvaluecondence)]
    return memoryvaluecondeser

那是课程 我需要从另一个python程序运行类函数,并且它需要具有可移植性,因此没有显式的文件夹名称,但是它们都位于同一文件夹中。

如何从另一个程序运行memory.getx("", Question)

1 个答案:

答案 0 :(得分:0)

课程需要以大写字母开头。

class Memory:
....code...

要在另一个程序中使用类,只需导入文件名 像这样:

import filename

mymemory = filename.Memory()