Python通过导入函数改变修改
我正在尝试创建一个函数,当导入然后调用时,它将检查并修改元组。我希望能够多次调用它。但是,我只是让函数返回新变量,因为我无法找出就地更改变量的方法。
这是我的带有两个文件的示例,我希望它如何工作:
**modifier.py**
import variable
def function(new_string):
if new_string not in variable.tuple:
variable.tuple = new_string, + variable.tuple
**variable.py**
import modifier
tuple = ('one','two',)
modifier.function('add this')
modifier.function('now this')
#--> tuple should now equal ('now this', 'add this', 'one', 'two',)
但是现在我必须这样做:
**modifier.py**
def function(tuple_old, new_string):
if new_string not in tuple_old:
return new_string, + tuple_old
**variable.py**
import modifier
tuple = ('one','two',)
tuple = modifier.function(tuple, 'add this')
tuple = modifier.function(tuple, 'now this')
#--> tuple now equals ('now this', 'add this', 'one', 'two',)
这更加混乱。首先,我必须传入旧的元组值并获取返回值,而不是直接替换元组。它可以工作,但它不干燥,我知道必须有一种方法可以使其更清洁。
我无法使用列表,因为这实际上是更新 django 设置文件上的中间件的函数。另外,我没有在不同的文件上拥有该功能,但我也认为这应该是可能的。
I am trying to create a function, that when imported and then called, it will check and modify a tuple. I would like to be able to call this multiple times. However, I just have the function return the new variable, because I can not figure out a way to change the variable in place.
Here is my examples with two files, how I would like it to work:
**modifier.py**
import variable
def function(new_string):
if new_string not in variable.tuple:
variable.tuple = new_string, + variable.tuple
**variable.py**
import modifier
tuple = ('one','two',)
modifier.function('add this')
modifier.function('now this')
#--> tuple should now equal ('now this', 'add this', 'one', 'two',)
However right now I have to do this:
**modifier.py**
def function(tuple_old, new_string):
if new_string not in tuple_old:
return new_string, + tuple_old
**variable.py**
import modifier
tuple = ('one','two',)
tuple = modifier.function(tuple, 'add this')
tuple = modifier.function(tuple, 'now this')
#--> tuple now equals ('now this', 'add this', 'one', 'two',)
This is a lot messier. First off, I have to pass in the old tuple value and get a returned value, instead of replacing the tuple directly. It works, but its not DRY and I know there must be a way to make this cleaner.
I can't use lists, because this is actually a function to update my middleware on my django settings file. Also I don't have to have the function on a different file, but I also think it should be possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为你现在所做的事情(最后一个代码块)有什么问题,很清楚。如果我看到类似的内容:
我知道元组已更改(可能只是您在示例中使用的名称,但不要将变量称为“元组”)。
但如果我看到这个(你想做的):
我永远不会想象
function
改变了tuple
的值。无论如何,它可以通过以下方式完成:也可以完成这样的事情:
我会说它更好一点,因为如果我要使用有问题的代码,我可能会猜测
function
对元组做了一些事情。代码将是:
最后我会说你现在所做的事情很清晰而且更简单,我不会改变它。
I don't see anything wrong about what you're doing right now (last code block), it's clear. If I see something like:
I know that tuple is changed (probably it's just a name you used for the example, but don't call your variable "tuple").
But if I see this (what you'd like to do):
I'd never imagine that
function
changed tha value oftuple
. Anyway, it could be done with:Also something like this could be done:
I'd say it's a little better because if I were to use your code having probelms I might guess that
function
does something to tuple.And the code would be:
At the end I'd say that what you're doing right now is clear and more simple, I wouldn't change it.
这似乎有效:
This seems to work: