简体   繁体   English

如何在Python中输出变化的参数?

[英]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. 现在的事情是,在调用此函数的情况下,这些变量( var1var2var3 )会有所不同。 So they could be var4 , var5 , var6 , etc. 因此它们可以是var4var5var6等。

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.

相关问题 Python:如何忽略函数中的参数? - Python: How can you ignore an argument in a function? 如何在Python中禁用socketserver的输出? - How can you disable the output of socketserver in Python? 如何在python中使用多位通配符来使用各种模式解析字符串? - How can you employ multi-digit wildcards in python to parse a string using varying patterns? 如何将Unix Shell参数的输出分配给Python中的变量 - How can assign the output of a Unix shell argument to a variable in Python Python-使用不同数量的空格格式化输出 - Python - formatting output with varying numbers of spaces 如何使Python等待以便您可以读取输出? - How do you make Python wait so that you can read the output? 您可以“导出” output 结果为 python 吗? - Can you 'export' output results in python? Python怎么解决这个问题? 从管理 class 调用时自动为 static 方法提供参数 - How can you solve this in Python? Automatically supply an argument to a static method when called from a managing class 如何在 python sqlite3 嵌套查询中两次传递相同的参数? - How can you pass the same argument twice in a python sqlite3 nested query? 在 python 枕头模块中编辑后如何保存具有不同名称和不同编号的图像 - How do you save images with a different name and varying numbers after editing in python pillow module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM