Discussion
Loading...

Post

Log in
  • About
  • Code of conduct
  • Privacy
  • About Bonfire
Paul McGuire
Paul McGuire
@[email protected]  ·  activity timestamp 12 hours ago

In retrospect, I should have known better than to do this, but I was hoping to avoid making a temporary copy. (SPOILER on why not in the replies) #python #gotchas

Python code illustrating a Python list gotcha:

def get_possible_words():
    return "YES NO Y N".split()

possible_yes_no = get_possible_words()

# add lower case options
possible_yes_no.extend(
    wd.lower() for wd in possible_yes_no  # <-- don't do this
)
Python code illustrating a Python list gotcha: def get_possible_words(): return "YES NO Y N".split() possible_yes_no = get_possible_words() # add lower case options possible_yes_no.extend( wd.lower() for wd in possible_yes_no # <-- don't do this )
Python code illustrating a Python list gotcha: def get_possible_words(): return "YES NO Y N".split() possible_yes_no = get_possible_words() # add lower case options possible_yes_no.extend( wd.lower() for wd in possible_yes_no # <-- don't do this )
1
  • Copy link
  • Flag this post
  • Block
Glyph
Glyph
@[email protected] replied  ·  activity timestamp 12 hours ago

@ptmcg lmao

tuple() can be your friend here, or at least annotating with Sequence

1
  • Copy link
  • Flag this comment
  • Block
Paul McGuire
Paul McGuire
@[email protected] replied  ·  activity timestamp 12 hours ago

@glyph Yeah, or if I just added `[:]` in the generator expression, I'd be iterate over a copy. I ended up making an explicit copy to a temporary variable, for future generations.

  • Copy link
  • Flag this comment
  • Block

bonfire.mavnn.eu

News and community around mavnn.eu projects.

bonfire.mavnn.eu: About · Code of conduct · Privacy ·
Bonfire social · 1.0.1 no JS en
Automatic federation enabled
Log in
  • Explore
  • About
  • Public Groups
  • Code of Conduct