Why do we use _ in variable names?

前端 未结 11 1816
一生所求
一生所求 2020-12-25 11:26

I have seen variables like _ image and was wondering what _ meant?

11条回答
  •  孤城傲影
    2020-12-25 11:57

    In most languages _ is the only character allowed in variable names besides letters and numbers. Here are some common use cases:

    • Separating words: some_variable
    • Private variables start with underscores: _private
    • Adding at the end to distinguish from a built-in name: filter_ (since filter is a built-in function)
    • By itself as an unused variable during looping: [0 for _ in range(n)]

    Note that some people really don't like that last use case.

提交回复
热议问题