在被调用的函数参数中访问另一个关键字是否可能?

huangapple go评论63阅读模式
英文:

Is it possible to access another keyword in called function parameter?

问题

抱歉,以下是翻译好的部分:

"我不太确定如何完全表达我在这里想知道的,所以请原谅任何混淆。

我想知道的是,一旦我导入一个模块并调用一个函数,是否可以在同一个函数内部访问另一个参数的值?

所以例如,我现在正在使用 pandas 玩耍,我已经用一段基本的代码创建了一个系列:

import pandas as pd

data = [40, 70, 90]
series = pd.Series(data=data, name='marks', index=[i for i in range(0, len(data))])

但我想看看是否可能像这样做:

import pandas as pd

series = pd.Series(data=[40, 70, 90], name='marks', index=[i for i in range(0, len(__get_value_from_data_keyword__))])

其中 __get_value_from_data_keyword__ 返回同一个函数中 data 关键字的值。所以 __get_value_from_data_keyword__ 等于 [40, 70, 90]

这是否有可能做到?"

英文:

I'm not sure how to completely articulate what I'm wondering here so forgive any confusion.

What I want to know is, Once I import a module and call a function is it possible to access another parameter's value inside another parameter within the same function?

So for example, I'm playing around with pandas at the moment and I've created a series with a basic block of code:

import pandas as pd

data = [40, 70, 90]
series = pd.Series(data=data, name='marks', index=[i for i in range(0, len(data))])

But I want to see if it's possible to do something like this:

import pandas as pd

series = pd.Series(data=[40, 70, 90], name='marks', index=[i for i in range(0, len(__get_value_from_data_keyword__))])

where __get_value_from_data_keyword__ returns the value of the data keyword in the same function. So __get_value_from_data_keyword__ equals [40, 70, 90]

Is this possible to do at all?

答案1

得分: 4

你可以使用赋值表达式来获取对“匿名”值的引用。

series = pd.Series(data=(x := [40, 80, 90]), name='marks', index=list(range(0, len(x))))

然而,这样的写法更易读:

x = [40, 80, 90]
series = pd.Series(data=x, name='marks', index=list(range(len(x))))
英文:

You can use an assignment expression to get a reference to the "anonymous" value.

series = pd.Series(data=(x := [40, 80, 90]), name='marks', index=list(range(0, len(x))))

However, this would be much more readable as

x = [40, 80, 90]
series = pd.Series(data=x, name='marks', index=list(range(x)))

答案2

得分: 0

在我理解你的问题时 - 不可以。
在Python中,你不能直接在同一个函数内部访问另一个参数的值。

英文:

As far as I understood your question - no.
In Python, you cannot directly access the value of a parameter inside another parameter within the same function.

huangapple
  • 本文由 发表于 2023年5月15日 01:04:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定