调用函数时将变量设置为内联参数值

发布于 2025-01-10 15:41:29 字数 572 浏览 1 评论 0原文

在其他语言(例如 Java)中,您可以执行类似的操作:

String path;
if (exists(path = "/some/path"))
    my_path = path;

重点是将 path 设置为指定方法调用参数的一部分。我知道这在 Python 中行不通。这是我一直希望 Python 拥有的东西。

有什么方法可以在Python中完成这个任务吗?我在这里所说的“完成”的意思是能够将对 exists 的调用和对 path 的赋值编写为单个语句,而无需事先支持代码。

如果执行此操作的方法需要使用对函数或方法的额外调用(包括我自己编写的任何内容),我会同意。我花了一点时间试图想出这样一个模块,但没有想出比在调用函数之前进行赋值更难看的东西。

更新:如果可以假设 Python 3.8 或更好,@BrokenBenchmark 的答案是完美的。不幸的是,我还不能做到这一点,所以我仍在寻找适用于 Python 3.7 及更早版本的这个问题的解决方案。

In other languages, like Java, you can do something like this:

String path;
if (exists(path = "/some/path"))
    my_path = path;

the point being that path is being set as part of specifying a parameter to a method call. I know that this doesn't work in Python. It is something that I've always wished Python had.

Is there any way to accomplish this in Python? What I mean here by "accomplish" is to be able to write both the call to exists and the assignment to path, as a single statement with no prior supporting code being necessary.

I'll be OK with it if a way of doing this requires the use of an additional call to a function or method, including anything I might write myself. I spent a little time trying to come up with such a module, but failed to come up with anything that was less ugly than just doing the assignment before calling the function.

UPDATE: @BrokenBenchmark's answer is perfect if one can assume Python 3.8 or better. Unfortunately, I can't yet do that, so I'm still searching for a solution to this problem that will work with Python 3.7 and earlier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

微凉 2025-01-17 15:41:29

是的,如果您使用的是 Python 3.8 或多于:

import os
if os.path.isdir((path := "/some/path")):
    my_path = path

Yes, you can use the walrus operator if you're using Python 3.8 or above:

import os
if os.path.isdir((path := "/some/path")):
    my_path = path
岁月打碎记忆 2025-01-17 15:41:29

我提出了一些有一些问题的东西,但从技术上讲确实让我达到了我想要的目标。也许其他人会有改进的想法,让它变得非常酷。这就是我所拥有的:

# In a utility module somewhere

def v(varname, arg=None):
    if arg is not None:
        if not hasattr(v, 'vals'):
            v.vals = {}
        v.vals[varname] = arg
    return v.vals[varname]

# At point of use

if os.path.exists(v('path1', os.path.expanduser('~/.harmony/mnt/fetch_devqa'))):
    fetch_devqa_path = v('path1')

如您所见,这符合我的要求,无需额外的代码行。所涉及的“变量”(本例中的 path1)基于每个变量名称存储在实现所有这些的函数中。

人们可能会质疑这是否足够简洁和可读,值得费心。对我来说,判决还没有出来。如果不是需要再次调用 v() 函数,我想我在结构上会很好用。

我看到的唯一功能问题是它不是线程安全的。代码的两个副本可以同时运行,并在两次调用 v() 之间遇到竞争条件。如果每次使用时都未能选择唯一的变量名称,那么同样的问题会大大放大。这可能是这里的交易杀手。

任何人都可以看到如何使用它来获得类似的解决方案而没有缺点吗?

I've come up with something that has some issues, but does technically get me where I was looking to be. Maybe someone else will have ideas for improving this to make it fully cool. Here's what I have:

# In a utility module somewhere

def v(varname, arg=None):
    if arg is not None:
        if not hasattr(v, 'vals'):
            v.vals = {}
        v.vals[varname] = arg
    return v.vals[varname]

# At point of use

if os.path.exists(v('path1', os.path.expanduser('~/.harmony/mnt/fetch_devqa'))):
    fetch_devqa_path = v('path1')

As you can see, this fits my requirement of no extra lines of code. The "variable" involved, path1 in this example, is stored on the function that implements all of this, on a per-variable-name basis.

One can question if this is concise and readable enough to be worth the bother. For me, the verdict is still out. If not for the need to call the v() function a second time, I think I'd be good with it structurally.

The only functional problem I see with this is that it isn't thread-safe. Two copies of the code could run concurrently and run into a race condition between the two calls to v(). The same problem is greatly magnified if one fails to choose unique variable names every time this is used. That's probably the deal killer here.

Can anyone see how to use this to get to a similar solution without the drawbacks?

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