you are viewing a single comment's thread
view the rest of the comments
[–] 42 points 1 year ago* (last edited 1 year ago) (4 children)

How does Python know if it's my list or not?

  • source
  • hideshow 8 child comments
  • [–] 7 points 1 year ago (2 children)

    if isinstance(mylist, list) and not mylist

    Problem solved.

    Or if not mylist # check if list is empty

  • source
  • parent
  • hideshow 4 child comments
  • [–] 4 points 1 year ago (1 child)

    You’re checking if mylist is falsey. Sometimes that’s the same as checking if it’s empty, if it’s actually a list, but that’s not guaranteed.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago (2 children)

    Doesn't Python treat all empty iterables as false tho? This isn't unique to python, is it? (though I'm not a programmer...just a dude who writes scripts every now and then)

  • source
  • parent
  • hideshow 4 child comments
  • [–] 3 points 1 year ago (1 child)

    My point is that the second statement you presented can have the effect of evaluating emptiness of a Sequence (note: distinct from an Iterable), but that only holds true if the target of the conditional IS a sequence. I’m underlining the semantic difference that was elided as a result of falsey evaluation.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 1 year ago (2 children)

    Ok, help a noob out. What is the difference between a sequence and an iterable? Is a sequence immutable, like a tuple?

  • source
  • parent
  • hideshow 4 child comments
  • [–] 1 point 1 year ago (1 child)

    An iterable is just something that can be iterated over, like range(10), or [1, 2, 3].

    A sequence on the other hand is a Collection that is reversible.

    https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes

  • source
  • parent
  • hideshow 2 child comments
  • thing: Sequence[Any] iirc is iterable, indexable, and reversible.

    thing: Iterable[Any] only guarantees that its iterable - and note that iterating can sometimes have the effect of consuming the iterable (e.g. when working with streaming interfaces)

  • source
  • parent