[英]How can you output a varying argument in Python?
I am writing a function in Python. 我正在用Python编写函数。
So lets say I have 3 arguments in this function: 所以可以说我在这个函数中有3个参数:
Function (arg1, arg2, arg3)
arg1 = arg2 * arg3
When the function is called, it is as such: 调用该函数时,它是这样的:
function(arg1=var1, arg2=var2, arg3=var3)
Now the thing is, these variables, ( var1
, var2
, var3
) vary in the case of this function being called. 现在的事情是,在调用此函数的情况下,这些变量( var1
, var2
, var3
)会有所不同。 So they could be var4
, var5
, var6
, etc. 因此它们可以是var4
, var5
, var6
等。
Given that these variables vary, what would be the syntax to make this function change a varying argument? 鉴于这些变量有所不同,使该函数更改可变参数的语法是什么?
ie. 即。 arg1 = arg2 * arg3
changes arg1
. arg1 = arg2 * arg3
更改arg1
。 Because arg1 = var1
. 因为arg1 = var1
。
What I need is somehow for arg1
to stand in as an actual proxy to var1
so that not only is arg1
changed but var1
is changed also. 我需要以某种方式使arg1
成为var1
的实际代理,这样不仅arg1
更改了,而且var1
也更改了。
This is evidently not doable with simply with arg1=var1
显然,仅使用arg1=var1
不可能实现
I assume this could also be done with other function commands or structure. 我认为这也可以通过其他功能命令或结构来完成。 How does not really concern me I just need to output a change into a varying argument. 我到底不怎么关心我,我只需要将更改输出为一个变量即可。
Edit This function is specifically designed to take an argument called, not necessarily knowing what it is, and then inside of the function, that argument which is converted into a formula variable such as x in y = mx + b, needs to be converted back into its variable target, all inside of the function. 编辑此函数专门用于接受一个称为的参数,而不必知道它是什么,然后在函数内部,需要将该参数转换回公式变量,例如y = mx + b中的x进入其可变目标,全部在函数内部。
The problem here is that I don't know how to target the original variable. 这里的问题是我不知道如何定位原始变量。
So if it were function(a=cats, b=dogs, c=mice) 所以如果它是函数(a = cats,b = dogs,c = mice)
a = c/2 - b*2 a = c / 2-b * 2
how do I then set cat=a when cat is variable? 当cat可变时,如何设置cat = a?
I can't just say cat=a because maybe the input is function(a=bird, b=flys, c=cats) 我不能只说cat = a,因为也许输入是函数(a = bird,b = flys,c = cats)
def f(x, y):
return x*y
a, b = 1, 2
c = f(a, b)
Instead of having your function try to alter its arguments directly, return any results the function produces. 返回让函数返回的所有结果,而不是让函数直接尝试更改其参数。 This turns out to be a much cleaner, more useful way to structure your code. 事实证明,这是一种结构更干净,更有用的方法。 For example, you can do 例如,您可以
print f(1, f(2, 3))
instead of having to create temporary variables: 无需创建临时变量:
f(a, 2, 3)
f(b, 1, a)
print b
Instead of doing 而不是做
f(arg1=var1, arg2=var2, arg3=var3)
f(arg1=var4, arg2=var5, arg3=var6)
do the following: 请执行下列操作:
var1 = f(var2, var3)
var4 = f(var5, var6)
If it is not doable with arg1=var1
, it's because of this: 如果使用arg1=var1
不能执行此操作,原因是:
Python passes references-to-objects by value (like Java), and everything in Python is an object. Python按值传递对对象的引用(例如Java),Python中的所有对象都是对象。 This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference... what's the deal? 这听起来很简单,但是随后您会注意到某些数据类型似乎表现出按值传递特征,而其他数据类型似乎表现为按引用传递……这是怎么回事?
It is important to understand mutable and immutable objects. 了解可变和不可变的对象很重要。 Some objects, like strings, tuples, and numbers, are immutable. 有些对象,例如字符串,元组和数字,是不可变的。 Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed. 在函数/方法内更改它们将创建一个新实例,并且该函数/方法外的原始实例不会更改。 Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. 列表和字典等其他对象是可变的,这意味着您可以就地更改对象。 Therefore, altering an object inside a function/method will also change the original object outside. 因此,在函数/方法内部更改对象也会在外部更改原始对象。
Why not just return arg1
and set val1
to it? 为什么不只返回arg1
并将val1
设置为呢?
Python doesn't have a specific syntax regarding references (not that I know of at least). Python没有关于引用的特定语法(至少我不知道)。 If you want your argument to be passed as reference to a function, it has to be mutable (eg. list, dictionary). 如果您希望将参数作为对函数的引用进行传递,则它必须是可变的(例如,列表,字典)。
def foobar(a, b):
a[0] = 42 # This will modify the list
b[0] = '-' # This will modify the value of b passed to the function
foobar(
[4,8,15,16,23,42], # This argument is passed by reference, mutable
'asdf' # This one is passed by value, immutable
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.