Python通过导入函数改变修改

发布于 2024-12-29 02:47:26 字数 1046 浏览 0 评论 0原文

我正在尝试创建一个函数,当导入然后调用时,它将检查并修改元组。我希望能够多次调用它。但是,我只是让函数返回新变量,因为我无法找出就地更改变量的方法。

这是我的带有两个文件的示例,我希望它如何工作:

**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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青萝楚歌 2025-01-05 02:47:27

我不认为你现在所做的事情(最后一个代码块)有什么问题,很清楚。如果我看到类似的内容:

tuple = # something ...

我知道元组已更改(可能只是您在示例中使用的名称,但不要将变量称为“元组”)。

但如果我看到这个(你想做的):

tuple = 'one', two'
function('add this')

我永远不会想象 function 改变了 tuple 的值。无论如何,它可以通过以下方式完成:

tuple = 'one', 'two'

def function(string):
    global tuple
    if new_string not in tuple:
        tuple = (new_string,) + tuple

function('add this')

也可以完成这样的事情:

tuple = 'one', two'
function(tuple, 'add this')

我会说它更好一点,因为如果我要使用有问题的代码,我可能会猜测 function 对元组做了一些事情。

代码将是:

tuple = 'one', 'two'

def function(old_tuple, string):
    global tuple
    if new_string not in old_tuple:
        tuple = (new_string,) + old_tuple

function(tuple, 'add this')

最后我会说你现在所做的事情很清晰而且更简单,我不会改变它。

I don't see anything wrong about what you're doing right now (last code block), it's clear. If I see something like:

tuple = # something ...

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):

tuple = 'one', two'
function('add this')

I'd never imagine that function changed tha value of tuple. Anyway, it could be done with:

tuple = 'one', 'two'

def function(string):
    global tuple
    if new_string not in tuple:
        tuple = (new_string,) + tuple

function('add this')

Also something like this could be done:

tuple = 'one', two'
function(tuple, 'add this')

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:

tuple = 'one', 'two'

def function(old_tuple, string):
    global tuple
    if new_string not in old_tuple:
        tuple = (new_string,) + old_tuple

function(tuple, 'add this')

At the end I'd say that what you're doing right now is clear and more simple, I wouldn't change it.

夜雨飘雪 2025-01-05 02:47:27

这似乎有效:

def function(new_string):
if new_string not in variable.tuple:
    variable.tuple = (new_string,) + variable.tuple

This seems to work:

def function(new_string):
if new_string not in variable.tuple:
    variable.tuple = (new_string,) + variable.tuple
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文