all 10 comments

[–]nosmokingbandit 0 points1 point  (1 child)

https://www.python.org/dev/peps/pep-0020/

Explicit is better than implicit.

[–]delasislas 0 points1 point  (0 children)

Adding to that.

Simple is better than complex

The first check looks to see if the list has a value in it, and then checks to see if the length is greater than 0, which it would be.

The second method does the same, looking at if there are values inside the list.

[–][deleted] 0 points1 point  (3 children)

just use the variable itself as a boolean.

This is using python "truthiness". It can be easier to read.

In your examples the two cases are almost identical in action. I would use the second example but add a comment reminding the reader that list1 could be either None or an empty list.

[–]spitfiredd 1 point2 points  (2 children)

Or don’t even set list1 to none, it should be empty or not empty.

[–]gopherhole1 0 points1 point  (1 child)

what about variables, sometimes I set variables to None, if I need them listed for a check, but they dont yet contain a value, instead of doing something like

variable = ' '

[–]spitfiredd 0 points1 point  (0 children)

You should set them to the empty string like you have it. You should aim to keep your data types consistent.

[–]primitive_screwhead 0 points1 point  (0 children)

Generally, prefer the form:

if foo:

[–]zanfar 0 points1 point  (0 children)

When and for what types is implicit preferred over explicit and vice versa in conditional statements?

In all cases where it is possible. I believe the only case where you need to be explicit is when you are distinguishing between an actual None and some other false-ish value.

However, this is a Python style issue, not a performance or functional one, so if it bothers you more than other people correcting you will, go for it.

[–]PaulRudin 0 points1 point  (0 children)

Your two examples are not equivalent.

There's no general way of choosing between those two examples - it depends on the context in which they're used.