简体   繁体   English

为什么在函数参数中使用常规?

[英]Why tuple convention in function parameters?

I was wondering why many functions - especially in numpy - utilize tuples as function parameters? 我想知道为什么许多函数 - 特别是在numpy中 - 使用元组作为函数参数?

eg: 例如:

a = numpy.ones( (10, 5) )

What could possibly be the use for that? 有什么可能用于此? Why not simply have something such as the following, since clearly the first parameters will always denote the size of the array? 为什么不简单地拥有如下内容,因为第一个参数总是表示数组的大小?

a = numpy.ones(10, 5)

Is it because there might be additional parameters, such as dtype? 是因为可能有其他参数,例如dtype? even if so, 即便如此,

a = numpy.ones(10, 5, dtype=numpy.int)

seems much cleaner to me, than using the convoluted tuple convention. 对我来说似乎比使用复杂的元组约定更清洁。

Thanks for your replies 谢谢你的回复

Because you want to be able to do: 因为你希望能够做到:

a = numpy.ones(other_array.shape)

and other_array.shape is a tuple. other_array.shape是一个元组。 There are a few functions that are not consistent with this and work as you've described, eg numpy.random.rand() 有一些函数与此不一致并按照您的描述工作,例如numpy.random.rand()

I think one of the benefits of this is that it can lead to consistency between the various methods. 我认为这样做的好处之一是它可以导致各种方法之间的一致性。 I'm not that familiar with numpy, but it would seem to me the first use case that comes to mind is if numpy can return the size of an array, that size, as one variable, can be directly passed to another numpy method, without having to know anything about the internals of how that size item is built. 我对numpy并不熟悉,但在我看来,第一个用例是如果numpy可以返回一个数组的大小,那个大小,作为一个变量,可以直接传递给另一个numpy方法,无需了解该大小项目的内部构造。

The other part of it is that size of an array may have two components but it's discussed as one value, not as two. 它的另一部分是数组的大小可能有两个组件,但它被讨论为一个值,而不是两个。

My guess: this is because in functions like np.ones , shape can be passed as a keyword argument when it's a single value. 我的猜测:这是因为在像np.ones这样的np.ones ,当它是单个值时, shape可以作为关键字参数传递。 Try 尝试

np.ones(dtype=int, shape=(2, 3))

and notice that you get the same value as you would have gotten from np.ones((2, 3), dtype=int) . 并注意到你获得的值与从np.ones((2, 3), dtype=int)得到的值相同。

[This works in Python more generally: [这更普遍适用于Python:

>>> def f(a, b):
...     return a + b
... 
>>> f(b="foo", a="bar")
'barfoo'

] ]

In order for python to tell the difference between foo(1, 2) , foo(1, dtype='int') and foo(1, 2, dtype='int') you would have to use keyword-only arguments which weren't formally introduced until python 3. It is possible to use **kargs to implement keyword only arguments in python 2.x but it's unnatural and does not seem Pythonic. 为了让python告诉foo(1, 2)foo(1, dtype='int')foo(1, 2, dtype='int')之间的区别foo(1, 2)你必须使用仅限关键字的参数直到python 3才正式引入。可以使用**kargs在python 2.x中实现仅关键字参数,但它不自然,似乎不是Pythonic。 I think for that reason array does not allow array(1, 2) but reshape(1, 2) is ok because reshape does not take any keywords. 我认为因为这个原因array不允许array(1, 2)reshape(1, 2)是可以的,因为reshape不带任何关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回某些参数的元组 function - Tuple function that returns certain parameters 如何将元组作为 function 的参数传递 - How to pass tuple as parameters of a function Python:函数参数作为具有单个输出的元组 - Python: function parameters as a tuple with single output 如何以列表或元组的形式输入参数? - How to enter parameters to function in form of a list or a tuple? 返回由 function 调用的 2 个参数组合的新元组 - Return a new tuple with combination of 2 parameters called by an function 要求python function参数中一定长度的元组 - Require a tuple with a certain length in python function parameters 我们如何在Python 3.x中处理元组参数和reduce函数? - How we treat tuple parameters and the reduce function in Python 3.x? 解包元组以在python 2中自动运行具有未知长度参数的函数 - Unpacking tuple to autorun a function with parameters of unknown length in python 2 将多个参数传递给多个元组集中的函数-python - pass multiple parameters to function in multiple tuple sets - python Python-为什么2个不同的函数将相同的元组标识为 <class 'tuple'> 而另一个函数将相同的元组标识为None - Python - Why 2 different functions identify the same tuple as <class 'tuple'> while another function identifies the same tuple as None
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM