从另一个文件导入和更改变量 [英] Importing and changing variables from another file

查看:28
本文介绍了从另一个文件导入和更改变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的...

我已经搜索并寻找直接回答我的问题的答案,但没有成功.我的问题很简单,老实说,我认为那里会有更直接的答案.请记住,我对这门语言还比较陌生,还在学习中.

I have searched and searched looking for an answer that directly answers my question, but have had no success. My problem is pretty straight forward and I honestly thought there would have been a more direct answer out there. Please keep in mind I am still relatively new to the language, and am still learning.

所以我将使用 fileAfileB 作为我的两个文件,以及 x 作为我的示例变量.变量 x 包含在 fileB 中.我如何将变量 x 导入 fileAthen 通过 raw_input 将 x 更改为另一个值然后用新值更新fileB中的变量x?

So I will use fileA and fileB as my two files, and x as my example variable. Variable x is contained in fileB. How do I go about importing variable x into fileA, then change x to another value via raw_input, and then have the variable x update in fileB with the new value?

我不确定这是否可能,但我肯定会这么认为.我使用的是 python 2.7.11,先谢谢你.

I'm not sure if this is even possible, but I would sure like to think so. I am using python 2.7.11, thank you in advanced.

推荐答案

如果您所指的变量"是可变值,那么您所要求的就会起作用.

If the "variable" you're referring to is an mutable value, what you're asking for will just work.

文件B:

my_variable = ["a list with a string in it"]

文件A:

from fileB import my_variable  # import the value
my_variable.append("and another string")

fileA 加载后 fileB.my_variable 将有两个值.

After fileA has been loaded fileB.my_variable will have two values in it.

但是,这只适用于可变值.如果变量是不可变的,fileA 中的代码不能就地改变它,所以你会遇到问题.没有办法直接解决这个问题,但有很多方法可以解决这个问题,并且仍然可以得到你想要的结果.

But, that only works for mutable values. If the variable is immutable, the code in fileA can't change it in place, and so you'll have issues. There's no way to directly fix that, but there are many ways to work around the issue and still get at what you want.

最简单的方法就是使用 import fileB 而不是 from fileB import my_variable.这让您可以在 fileB 的命名空间中使用任何名称,只需使用类似 fileB.whatever 的名称.您可以将命名空间中的内容重新绑定到您心中的内容:

The easiest will simply be to use import fileB instead of from fileB import my_variable. This lets you anything in fileB's namespace, just by using a name like fileB.whatever. You can rebind things in the namespace to your heart's content:

文件B:

my_variable = 1    # something immutable this time

文件A:

import fileB
fileB.my_variable = 2   # change the value in fileB's namespace

这可能是最简单的方法.

That is probably the simplest approach.

另一种解决方案是将不可变变量放入可变容器中,然后修改容器而不是变量.例如,如果字符串 "a list with a string in it" 是我们想要更改第一个示例的值,我们可以简单地为 my_variable[0]<分配一个新值/code>(而不是附加).

Another solution would be to put the immutable variable inside a mutable container, and then modify the container, rather than the variable. For instance, if the string "a list with a string in it" was the value we wanted to change my the first example, we could simply assign a new value to my_variable[0] (rather than appending).

一种常见的方法是将值放入字典、列表甚至类(或一个的可变实例)中.然后你就可以导入容器对象并对其进行变异以改变你关心的值.

A common way to do this is to put values into a dictionary, list or even in a class (or a mutable instance of one). Then you can import the container object and mutate it to change the value you care about.

这篇关于从另一个文件导入和更改变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆