While I'm going through Python code and seeing functions called, I notice things like
当我正在浏览Python代码并看到调用的函数时,我注意到了类似的东西
functionCall(argument='something')
or
要么
someclass.functionCall(argument='something')
I've played around with it and noticed you have to name that variable with the same name as the one in the scope of the function or class function itself. Is that just a convention (for useful naming) or is there more to it that I'm missing?
我玩过它并注意到你必须将该变量命名为与函数或类函数本身范围内的变量同名。这只是一个约定(对于有用的命名)还是我缺少的更多?
4 个解决方案
#1
10
Those are just standard keyword arguments.
这些只是标准的关键字参数。
They are mainly useful when calling functions that usually assume default values, but the user is interested in passing a custom value without affecting the other defaults. For example:
它们在调用通常采用默认值的函数时非常有用,但用户有兴趣传递自定义值而不影响其他默认值。例如:
def foo(a='a', b='b', c='c'):
print a + b + c
Then:
然后:
>>> foo()
'abc'
>>> foo(c='x') # don't know or care about a and b's value
'abx'
#2
2
This is called a keyword argument. Keyword arguments have a couple distinct advantages:
这称为关键字参数。关键字参数有几个明显的优点:
- They can be specified in any order by the caller.
- 它们可以由调用者以任何顺序指定。
- They have default values, which means the caller doesn't have to provide a value for each argument if the defaults are sensible.
- 它们具有默认值,这意味着如果默认值是合理的,则调用者不必为每个参数提供值。
- They can make code more readable. For example,
foo(width=100, height=200)
is easier to understand thanfoo(100, 200)
. It is also easier to use, because you don't have to remember whether the function takes width,height or height,width. - 它们可以使代码更具可读性。例如,foo(width = 100,height = 200)比foo(100,200)更容易理解。它也更容易使用,因为您不必记住该函数是否采用宽度,高度或高度,宽度。
#3
2
It is good to have named arguments as arguments can be specified in any order by using named arguments.
具有命名参数是好的,因为可以使用命名参数以任何顺序指定参数。
Even required arguments (like object, which has no default value) can be named, and named arguments can appear in any order.
甚至可以命名所需的参数(如对象,没有默认值),命名参数可以按任何顺序出现。
另见This
Python's argument passing mechanisms are extremely flexible.
Python的参数传递机制非常灵活。
cons: too many arguments to a function. Solutions: split into multiple functions, pass some args together in a dictionary or object.
缺点:函数的参数太多了。解决方案:分成多个函数,在字典或对象中将一些args传递到一起。
cons: bad variable names. Solution: give variables more descriptive names.
缺点:错误的变量名称。解决方案:为变量赋予更多描述性名称。
Or remember the correct order .. :)
或者记住正确的订单.. :)
class xyz:
def __init__ (self, a='1', b='2'):
print a,b
xyz(b=3,a=4)
xyz(a=5,b=6)
>>4 3
>>5 6
#4
1
these are keyword parameters. They enable you to pass parameters in any order (useful for skipping optional parameters)
这些是关键字参数。它们使您能够以任何顺序传递参数(对跳过可选参数很有用)
Please note though, calling functions that way comes with a little bit of overhead:
请注意,调用函数会带来一些开销:
def test(a,b,c):
return a+b+c
def t1():
return test(1,2,3)
def t2():
return test(a=1, b=2, c=3)
timeit(t1)
0.23918700218200684
timeit(t2)
0.2716050148010254
#1
10
Those are just standard keyword arguments.
这些只是标准的关键字参数。
They are mainly useful when calling functions that usually assume default values, but the user is interested in passing a custom value without affecting the other defaults. For example:
它们在调用通常采用默认值的函数时非常有用,但用户有兴趣传递自定义值而不影响其他默认值。例如:
def foo(a='a', b='b', c='c'):
print a + b + c
Then:
然后:
>>> foo()
'abc'
>>> foo(c='x') # don't know or care about a and b's value
'abx'
#2
2
This is called a keyword argument. Keyword arguments have a couple distinct advantages:
这称为关键字参数。关键字参数有几个明显的优点:
- They can be specified in any order by the caller.
- 它们可以由调用者以任何顺序指定。
- They have default values, which means the caller doesn't have to provide a value for each argument if the defaults are sensible.
- 它们具有默认值,这意味着如果默认值是合理的,则调用者不必为每个参数提供值。
- They can make code more readable. For example,
foo(width=100, height=200)
is easier to understand thanfoo(100, 200)
. It is also easier to use, because you don't have to remember whether the function takes width,height or height,width. - 它们可以使代码更具可读性。例如,foo(width = 100,height = 200)比foo(100,200)更容易理解。它也更容易使用,因为您不必记住该函数是否采用宽度,高度或高度,宽度。
#3
2
It is good to have named arguments as arguments can be specified in any order by using named arguments.
具有命名参数是好的,因为可以使用命名参数以任何顺序指定参数。
Even required arguments (like object, which has no default value) can be named, and named arguments can appear in any order.
甚至可以命名所需的参数(如对象,没有默认值),命名参数可以按任何顺序出现。
另见This
Python's argument passing mechanisms are extremely flexible.
Python的参数传递机制非常灵活。
cons: too many arguments to a function. Solutions: split into multiple functions, pass some args together in a dictionary or object.
缺点:函数的参数太多了。解决方案:分成多个函数,在字典或对象中将一些args传递到一起。
cons: bad variable names. Solution: give variables more descriptive names.
缺点:错误的变量名称。解决方案:为变量赋予更多描述性名称。
Or remember the correct order .. :)
或者记住正确的订单.. :)
class xyz:
def __init__ (self, a='1', b='2'):
print a,b
xyz(b=3,a=4)
xyz(a=5,b=6)
>>4 3
>>5 6
#4
1
these are keyword parameters. They enable you to pass parameters in any order (useful for skipping optional parameters)
这些是关键字参数。它们使您能够以任何顺序传递参数(对跳过可选参数很有用)
Please note though, calling functions that way comes with a little bit of overhead:
请注意,调用函数会带来一些开销:
def test(a,b,c):
return a+b+c
def t1():
return test(1,2,3)
def t2():
return test(a=1, b=2, c=3)
timeit(t1)
0.23918700218200684
timeit(t2)
0.2716050148010254