让我们说我有一个可以采用各种参数值的函数,但我不想(作为约束)明确地传递参数。相反,我想将它们作为字符串传递。:
def func(param)
return param+param
a = 'param=4'
func(<do something to a>(a))
>>8
这在python中是否可行?
我想在Django中使用这个想法来创建基于字典中的GET参数的查询过滤器,然后使用它们的链来链接它们。
lookup_dic = {'user': 'author=user',
'draft': 'Q(publish_date_lte=timezone.now())|
Q(publish_date_isnull=True)'}
根据用户和草稿关键字是否在GET
参数中传递,这将被读出:
queryset.objects.filter(author=user).filter(Q(publish_date_lte=timezone.now())|
Q(publish_date_isnull=True))
我知道我可以通过将author=user
替换为Q(author__name=user)
来实现此目的,但我想知道这个字符串理解功能是否在python中实现了?
答案 0 :(得分:1)
使用-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ProudpartnersViewCell *myCell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"cell"
forIndexPath:indexPath];
myCell.layer.masksToBounds = NO;
myCell.layer.shadowRadius = 5;
myCell.layer.shadowOffset = CGSizeMake(0.0, 3.0);
myCell.layer.shadowOpacity = 0.2
NSString *strImg_Path = [[NSString alloc]init];
NSString *arrayResult = [imageOfArray objectAtIndex:indexPath.row]; // error is here
strImg_Path = [NSString stringWithFormat:@"%@",[NSURL URLWithString:arrayResult]];
[myCell.proudOfImages sd_setImageWithURL:[NSURL URLWithString:strImg_Path]
placeholderImage:[UIImage imageNamed:@"placeholer_image"]];
return myCell;
}
eval
答案 1 :(得分:1)
你在找这个吗?
def func(param):
return param + param
a = 'param=4'
parameter, value = a.split("=")
print(func(**{parameter: int(value)}))
# >> 8