Just started as in, I’m about an hour into a 4 hour intro video. Seeing two basic ways of manipulating things and don’t understand the difference.

If I want to know the length of a string and I just guess at how to do it I would try one of these two things,

  1. Len(string)
  2. string.len()

What is the difference between these types of statements? How do I think about this to know which one I should expect to work?

  • theneverfox@pawb.social
    link
    fedilink
    arrow-up
    1
    ·
    3 months ago

    There are definitely ways, IIRC str.len() is a function, but the double underscores basically mean “please don’t call this directly”. There might be other “hidden” variables tracking it, and if you wanted you could add a .length method to the string class at runtime

    The beauty of python is it’s pretty consistent in giving you one correct way to do things with the language, it will let you shoot yourself in the foot if you want to though