给出一个简单的功能:
def A(a = 1, b = 2):
return a+b
我想编写另一个函数来更改a
或b
的默认参数值。用户可以通过设置var = a
或var = b
来指定要更改的参数。例如:
def B(var = 'a', new_value = 10):
temp_dict = {var:new_value}
ans = A(var)
return ans
或
def B(var = 'a', new_value = 10):
ans = A(var = new_value)
return ans
在功能def B()
中,设置var = a
和var = new_value = 10
后,我希望A(var = new_value)
达到与A(a = 10)
相同的效果。你知道写函数def B()
的正确方法吗?
谢谢。
答案 0 :(得分:5)
你快到了。在6395ms
函数中,在调用B()
时,您需要解压缩A()
并将其作为参数传递给temp_dict
。见下文:
A()
有关此>>> def A(a = 1, b = 2):
... return a+b
...
>>> def B(var = 'a', new_value = 10):
... temp_dict = {var:new_value}
... ans = A(**temp_dict)
# ^ unpack the dict, and pass it as an argument
... return ans
...
>>> B()
12
如何与词典一起使用的详细信息,请查看:
答案 1 :(得分:0)
我冒昧地解释了OP所说的他想要的东西,即更改默认参数值,a或b 。所以我做的是返回一个转换后的函数A,其中a或b默认值通过partial:
更改func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .normal, title: "Delete") { action, view, completionHandler in
print("Deleting!")
completionHandler(false)
}
delete.backgroundColor = UIColor.red
let config = UISwipeActionsConfiguration(actions: [delete])
config.performsFirstActionWithFullSwipe = false
return config
}
样本输出:
*** Assertion failure in -[UISwipeActionController swipeHandlerDidBeginSwipe:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3694.4.18/SwipeActions/UISwipeActionController.m:268
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'No occurrence for index path (null)'
这与请求的后半部分有所不同,即基于 $sql2 = "SELECT trans_details.*, company.comp_name FROM trans_details INNER JOIN company ON trans_details.comp_id=company.comp_id WHERE trans_details.comp_id = '$company'
and trans_date BETWEEN CAST('$startdate' as date) and CAST('$enddate' as date)";
$stmt2 = sqlsrv_query($conn, $sql2);
if($stmt2 ===FALSE){
die(print_r(sqlsrv_errors(), TRUE));
}else{
echo "<center><h3>CANTEEN TRANSACTION LISTING<br>Summary By Company</h3>
<strong>Date From : </strong>".$datestart." <strong>To : </strong>".$dateend."<br>";
?>
<center><table border="1" cellpadding="4px">
<thead>
<tr>
<th>Cashier#</th>
<th>Total Txn</th>
<th>Total Amount (RM)</th>
<th>Total Cash (RM)</th>
<th>Total Subsidy (RM)</th>
</tr>
</thead>
<tbody>
<?php
//count transaction
$compname2 = "SELECT * from company where comp_id='$company'";
$compp = sqlsrv_query($conn, $compname2);
while ($c =sqlsrv_fetch_array($compp, SQLSRV_FETCH_ASSOC))
{
echo "<center><strong> Company :</strong>".$c['comp_name']."</center><br><br>";
}
$txnT = 0;
$sumAmount = 0;
$sumCash = 0;
$sumSubsidy = 0;
$txn = "SELECT user_id,count(1) as txnT,sum(CAST(amount AS DECIMAL(12,2))) as sumAmount,
sum(CAST(cash AS DECIMAL(12,2))) as sumCash,sum(CAST(subsidy AS DECIMAL(12,2))) as sumSubsidy
from trans_details where trans_date BETWEEN CAST('$startdate' as date) and CAST('$enddate' as date) group by user_id";
$res = sqlsrv_query($conn, $txn);
//count end
while($row2 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) //fetching main query
{
while($num = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC)) //fetching 1st condition
{
echo "<center><tr><td>".$num['user_id']."</td>"
. "<td align='right'>".$num['txnT']."</td>"
. "<td align='right'>".$num['sumAmount']."</td>"
. "<td align='right'>".$num['sumCash']."</td>"
. "<td align='right'>".$num['sumSubsidy']."</td>"
. "</tr>";
}
}
}
//sum up all
$ttl = "SELECT count(1) as Txn,sum(CAST(amount as DECIMAL(12,2))) as ttlamount,
sum(CAST(cash AS DECIMAL(12,2))) as ttlcash,sum(CAST(subsidy AS DECIMAL(12,2))) as ttlsubsidy
from trans_details where trans_date BETWEEN CAST('$startdate' as date) and CAST('$enddate' as date)";
$re = sqlsrv_query($conn, $ttl);
while($num_row = sqlsrv_fetch_array($re, SQLSRV_FETCH_ASSOC)) //fetching 2nd condition
{
echo "<tr>
<td><strong>SUB TOTALS</strong></td>"
."<td align='right'><strong>".$num_row['Txn']."</strong></td>"
."<td align='right'><strong>".$num_row['ttlamount']."</strong></td>"
."<td align='right'><strong>".$num_row['ttlcash']."</strong></td>"
."<td align='right'><strong>".$num_row['ttlsubsidy']."</strong></td>"
."</tr></tbody></center>";
}
的某些内容作为函数签名。
唯一的问题是,如果你不使用关键字参数,它就会很开心:
while($num = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC))