python - 我需要从 python 中的另一个程序导入一个类
问题描述
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)
从另一个程序运行
解决方案
类需要以大写字母开头。
class Memory:
....code...
要在另一个程序中使用一个类,你只需要像这样导入文件名:
import filename
mymemory = filename.Memory()