Why does python use two underscores for certain things?

前端 未结 7 1388
滥情空心
滥情空心 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:19

    Well, power for the programmer is good, so there should be a way to customize behaviour. Like operator overloading (__add__, __div__, __ge__, ...), attribute access (__getattribute__, __getattr__ (those two are differnt), __delattr__, ...) etc. In many cases, like operators, the usual syntax maps 1:1 to the respective method. In other cases, there is a special procedure which at some point involves calling the respective method - for example, __getattr__ is only called if the object doesn't have the requested attribute and __getattribute__ is not implemented or raised AttributeError. And some of them are really advanced topics that get you deeeeep into the object system's guts and are rarely needed. So no need to learn them all, just consult the reference when you need/want to know. Speaking of reference, here it is.

提交回复
热议问题