"关键字参数"与常规参数有何不同?不能传递所有参数name=value而不是使用位置语法?
python arguments keyword optional-parameters named-parameters
我发现C#中的命名参数功能在某些情况下非常有用.
calculateBMI(70, height: 175);
Run Code Online (Sandbox Code Playgroud)
如果我想在javascript中使用该怎么办?
我不想要的是 -
myFunction({ param1: 70, param2: 175 });
function myFunction(params){
// Check if params is an object
// Check if the parameters I need are non-null
// Blah blah
}
Run Code Online (Sandbox Code Playgroud)
我已经使用过这种方法.还有另外一种方法吗?
我可以使用任何库来做这件事.(或者有人可以指出我已经做过的那个)
我想我可以在Python 2的函数调用中使用可变长度位置参数之后的命名参数,但是SyntaxError在导入python类时我得到了一个.我正在使用以下"get"方法编写,例如:
class Foo(object):
def __init__(self):
print "You have created a Foo."
def get(self, *args, raw=False, vars=None):
print len(args)
print raw
print vars
Run Code Online (Sandbox Code Playgroud)
错误看起来像:
def get(self, *args, raw=False, vars=None):
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我希望能够通过几种方式调用该方法:
f = Foo()
f.get(arg1, arg2)
f.get(arg1, raw=True)
f.get(arg1, arg2, raw=True, vars=something)
Run Code Online (Sandbox Code Playgroud)
等等
python python-2.x variadic-functions named-parameters default-parameters
是否有一个名为JDBC中,而不是那些位置参数,比如@name,@city在下面的ADO.NET查询?
select * from customers where name=@name and city = @city
Run Code Online (Sandbox Code Playgroud) 我在一个类中有这个函数:
func multiply(factor1:Int, factor2:Int) -> Int{
return factor1 * factor2
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用这个来调用函数:
var multResult = calculator.multiply(9834, 2321)
Run Code Online (Sandbox Code Playgroud)
问题是编译器希望它看起来更像这样:
var multResult = calculator.multiply(9834, factor2: 2321)
Run Code Online (Sandbox Code Playgroud)
为什么第一个会导致错误?
我有方法
def test(String a, String b) { }
Run Code Online (Sandbox Code Playgroud)
我想用动态参数图来调用它.我总是这样
test(['1','2']); //valid call
Run Code Online (Sandbox Code Playgroud)
并且
test([a:'1',b:'2']); //=> does not work
Run Code Online (Sandbox Code Playgroud)
将工作.但事实并非如此.所以我记得传播操作符,但无法让它工作....
有没有办法用某种地图作为参数而不是单个参数调用上面的方法?
我如何指定JPA查询,如:
Query q =
em.createQuery(
"SELECT x FROM org.SomeTable x WHERE x.someString LIKE '%:someSymbol%'"
);
Run Code Online (Sandbox Code Playgroud)
其次是:
q.setParameter("someSymbol", "someSubstring");
Run Code Online (Sandbox Code Playgroud)
而不是触发一个
org.hibernate.QueryParameterException: could not locate named parameter [id]
Run Code Online (Sandbox Code Playgroud)
非常感激!
我正在研究Scala API(顺便说一下,对于Twilio),其中操作具有相当多的参数,其中许多具有合理的默认值.为了减少输入并增加可用性,我决定使用带有命名和默认参数的case类.例如对于TwiML Gather动词:
case class Gather(finishOnKey: Char = '#',
numDigits: Int = Integer.MAX_VALUE, // Infinite
callbackUrl: Option[String] = None,
timeout: Int = 5
) extends Verb
Run Code Online (Sandbox Code Playgroud)
这里感兴趣的参数是callbackUrl.它是唯一真正可选的参数,如果没有提供任何值,则不会应用任何值(这是完全合法的).
我已经宣布它是一个选项,以便在API的实现方面使用它来执行monadic map例程,但这给API用户带来了额外的负担:
Gather(numDigits = 4, callbackUrl = Some("http://xxx"))
// Should have been
Gather(numDigits = 4, callbackUrl = "http://xxx")
// Without the optional url, both cases are similar
Gather(numDigits = 4)
Run Code Online (Sandbox Code Playgroud)
据我所知,有两种选择(没有双关语)来解决这个问题.要么让API客户端导入隐式转换到范围:
implicit def string2Option(s: String) : Option[String] = Some(s)
Run Code Online (Sandbox Code Playgroud)
或者我可以使用null默认值重新声明case类,并将其转换为实现端的选项:
case class Gather(finishOnKey: Char = '#',
numDigits: Int = …Run Code Online (Sandbox Code Playgroud) 我查看了Named Parameter Idiom和Boost :: Parameter库.每个人有什么优势?是否有充分的理由总是选择其中一个,或者在某些情况下每个人都可能比另一个好(如果是的话,在什么情况下)?
编译这个简单的程序:
class Program
{
static void Foo( Action bar )
{
bar();
}
static void Main( string[] args )
{
Foo( () => Console.WriteLine( "42" ) );
}
}
Run Code Online (Sandbox Code Playgroud)
没什么奇怪的.如果我们在lambda函数体中出错:
Foo( () => Console.LineWrite( "42" ) );
Run Code Online (Sandbox Code Playgroud)
编译器返回一条错误消息:
error CS0117: 'System.Console' does not contain a definition for 'LineWrite'
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.现在,让我们在调用中使用命名参数Foo:
Foo( bar: () => Console.LineWrite( "42" ) );
Run Code Online (Sandbox Code Playgroud)
这一次,编译器消息有点令人困惑:
error CS1502: The best overloaded method match for
'CA.Program.Foo(System.Action)' has some invalid arguments
error CS1503: Argument 1: cannot convert from …Run Code Online (Sandbox Code Playgroud)