this post was submitted on 12 Apr 2025
195 points (90.1% liked)

Technology

69041 readers
2996 users here now

This is a most excellent place for technology news and articles.


Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related news or articles.
  3. Be excellent to each other!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. Only approved bots from the list below, this includes using AI responses and summaries. To ask if your bot can be added please contact a mod.
  9. Check for duplicates before posting, duplicates may be removed
  10. Accounts 7 days and younger will have their posts automatically removed.

Approved Bots


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

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

[–] [email protected] 28 points 1 week ago
[–] [email protected] 7 points 1 week ago (2 children)

if isinstance(mylist, list) and not mylist

Problem solved.

Or if not mylist # check if list is empty

[–] sirber 15 points 1 week ago (1 children)

I think you missed the joke 😅

[–] [email protected] 3 points 1 week ago

I thought it was funny!

[–] [email protected] 4 points 1 week ago (1 children)

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.

[–] [email protected] 1 points 1 week 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)

[–] [email protected] 3 points 1 week ago (1 children)

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.

[–] [email protected] 1 points 1 week 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?

[–] [email protected] 1 points 1 week ago (1 children)

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

[–] [email protected] 1 points 1 week ago

I know what an iterable is. But I am talking about Type[Iterable], which iirc does not obey falsey eval when empty.

[–] [email protected] 1 points 1 week ago* (last edited 1 week ago)

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)

[–] [email protected] 2 points 1 week ago

Not really, generators have weird truthiness, i don't remember if they evaluate to true or false, but they cannot be checked for emptiness so they default to either always true or always false.

[–] [email protected] 1 points 1 week ago

else: # not my list, it is ourlist

[–] [email protected] 1 points 1 week ago

Python likes giving lists.