检查属性是否存在并且在Python中是真实的

时间:2015-12-03 09:26:10

标签: python dictionary attributes

我是Python新手。我发现自己做了很多这样的事情:

listItemService.get(position).getScheduleOn().startwith(DateofTodayorTomorrow)
Calendar c=Calendar.getInstance();      
    c.setTimeInMillis(System.currentTimeMillis());      
    String dateOfToday=c.get(Calendar.YEAR)+"-"+(c.get(Calendar.MONTH)+1)+"-"+c.get(Calendar.DAY_OF_MONTH);     
    c.add(Calendar.DAY_OF_MONTH, 1);        
    String dateOfTomorrow=c.get(Calendar.YEAR)+"-"+(c.get(Calendar.MONTH)+1)+"-"+c.get(Calendar.DAY_OF_MONTH);

我使用它作为在PHP中检查$ _GET变量的等价物:

if 'input_url' in inputparams and inputparams['input_url']:
    # do something with inputparams['input_url']

if 'output_url' in inputparams and inputparams['output_url']:
    # do something with inputparams['output_url']

在Python中有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

如果找不到密钥,

dict.get(key)会返回None。因此,如果inputparamsdict,那么

if 'input_url' in inputparams and inputparams['input_url']:

可以简化为:

if inputparams.get('input_url'):