简体   繁体   English

如何在一个文件中调用使用另一个文件已更改的数据结构的方法?

[英]How can I call a method in one file that uses a data structure that has been changed by another file?

I wanted to call a method in one file that picks a random number from a list of numbers with a length defined by another file. 我想在一个文件中调用一个方法,该方法从由另一个文件定义的长度的数字列表中选择一个随机数。

Here's what I currently have: 这是我目前拥有的:

#file1
import file2

class Creator(object):
    def make_shape():
        return Shape(len(file2.sideList)) #takes a parameter of sides

shape = Creator.make_shape() #gets called after sideList is given a certain sequence

#file2
import random

sideList = []

for i in range(1, random.randint(1, 10)):
    sideList.append(i)

The obvious problem with the code above is that when file2 is imported, sideList is imported as [] instead of [1, 2, 3, etc]. 上面的代码的明显问题是,当导入file2时,sideList作为[]而不是[1、2、3等]导入。 So when the make_shape() method is called, the shape returned would be a shape with 0 sides. 因此,当调用make_shape()方法时,返回的形状将是带有0个边的形状。

So how could I "update" sideList in file2 so that I could call the make_shape() method using an "updated" version of file2.sideList? 那么,如何才能“更新” file2中的sideList,以便可以使用file2.sideList的“更新”版本来调用make_shape()方法?

Could you create a function in file 2 that accommodates your needs? 您能否在文件2中创建一个满足您需要的功能?

#file1.py
import file2

class Creator(object):
    def make_shape():
        return Shape(len(file2.sideList())) #takes a parameter of sides

shape = Creator.make_shape() #gets called after sideList is given a certain sequence

#file2.py
import random

def sideList():
    ret = []    
    for i in range(1, random.randint(1, 10)):
        ret.append(i)
    return(ret)

Edit: 编辑:

 #file2.py
 import random

 def sideList():
     dataTarget = "__Data__.csv"
     if os.path.isfile(dataTarget):
          fh = open(dataTarget)
          ret = fh.read().split("\n")
     else:
          ret = []    
          for i in range(1, random.randint(1, 10)):
              ret.append(i)
          fh = open(dataTarget)
          fh.write(ret.join(\n))
     fh.close()
     return(ret)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将已读取并解析的数据写入另一个文件? - How to write data that has been read and parsed into another file? 您能否轻松测试方法定义是否已更改,但对该方法的调用尚未更新(使用 Pytest 和 Mock)? - Can you easily test whether a method definition has changed, but a call to the method has not been updated (with Pytest and Mock)? 如何检查一个方法是否已调用另一个方法? - How to check in one method if another method has been called? 如何在具有文件目录参数并在另一个 python 文件中返回数组的方法中使用数组? - How can I make use an array in a method that has parameter of a file directory and that returns an array in another python file? 如何在python中另一个文件下的另一个类中调用一个方法? - How can I call a method which is in another class under another file in python? 检查动态网站上的文件是否已更改 - Check if file has been changed on dynamic website 当我运行一个调用另一个 python 文件时,如何将一些 arguments 传递给它? - when I run a python file which call another one ,how can I pass some arguments to it? 如何在另一个文件中定义的一个文件中调用 python function,两者都在一个主 python 文件中? - How can I call a python function in one file that is defined within another, with both being in one main python file? 如何从 Python 中的另一个文件调用类方法? - How do I call a class method from another file in Python? 如何修改一个文件中的变量并在另一个文件中使用它? - How can I modify a variable in one file and use it in another file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM