如何让编译器解释为bolean上下文

时间:2017-04-06 13:21:22

标签: python python-3.x

我写了以下功能:

(SELECT date FROM showing WHERE id_showing=showing_id_showing) "Date",
(SELECT title FROM film WHERE id_film=film_id_film) "Title",

该函数总是返回None,但我希望编译器在布尔上下文中解释它,就像使用if语句一样:

def has_voted(request, question):
    cookie = request.COOKIES.get(COOKIE_NAME)
    ip = get_client_ip(request)
    return ((cookie and re.match(COOKIE_PATTERN, cookie) and
            question.id in map(int, cookie.split('-'))) or
            (ip and is_valid_ip_address(ip) and
            question.voter_set.filter(ip=ip).exists()))

1 个答案:

答案 0 :(得分:2)

如果要返回布尔值,则返回布尔值:

return bool((cookie and re.match(COOKIE_PATTERN, cookie) and
            question.id in map(int, cookie.split('-'))) or
            (ip and is_valid_ip_address(ip) and
            question.voter_set.filter(ip=ip).exists()))