Why does python use two underscores for certain things?

前端 未结 7 1410
滥情空心
滥情空心 2020-11-27 15:43

I\'m fairly new to actual programming languages, and Python is my first one. I know my way around Linux a bit, enough to get a summer job with it (I\'m still in high school)

7条回答
  •  盖世英雄少女心
    2020-11-27 16:10

    [Speculation] Python was influenced by Algol68, Guido possibly used Algol68 at the University of Amsterdam where Algol68 has a similar "stropping regime" called "Quote stropping". In Algol68 the operators, types and keywords can appear in a different typeface (usually **bold**, or __underlined__), in sourcecode files this typeface is achieved with quotes, e.g. 'abs' (quoting similar to quoting in 'wikitext')

    Algol68 ⇒ Python (Operators mapped to member functions)

    • 'and' ⇒ __and__
    • 'or' ⇒ __or__
    • 'not' ⇒ not
    • 'entier' ⇒ __trunc__
    • 'shl' ⇒ __lshift__
    • 'shr' ⇒ __rshift__
    • 'upb' ⇒ __sizeof__
    • 'long' ⇒ __long__
    • 'int' ⇒ __int__
    • 'real' ⇒ __float__
    • 'format' ⇒ __format__
    • 'repr' ⇒ __repr__
    • 'abs' ⇒ __abs__
    • 'minus' ⇒ __neg__
    • 'minus' ⇒ __sub__
    • 'plus' ⇒ __add__
    • 'times' ⇒ __mul__
    • 'mod' ⇒ __mod__
    • 'div' ⇒ __truediv__
    • 'over' ⇒ __div__
    • 'up' ⇒ __pow__
    • 'im' ⇒ imag
    • 're' ⇒ real
    • 'conj' ⇒ conjugate

    In Algol68 these were refered to as bold names, e.g. abs, but "under-under-abs" __abs__ in python.

    My 2 cents: ¢ So sometimes — like a ghost — when you cut and paste python classes into a wiki you will magically reincarnate Algol68's bold keywords. ¢

提交回复
热议问题