What are Python namespaces all about

前端 未结 6 1250
抹茶落季
抹茶落季 2020-11-30 23:14

I have just started learning Python & have come across \"namespaces\" concept in Python. While I got the jist of what it is, but am unable to appreciate

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 00:20

    Namespaces prevent conflicts between classes, methods and objects with the same name that might have been written by different people.

    Coming from a Java background you are probably familiar with how this is achieved using packages e.g. you might create a movieyoda.DateUtils class and I can create a mikej.DateUtils class and the package allows code using the classes to distinguish between them. (Python has something very similar.)

    Namespaces were added to PHP in 5.3.0 but in earlier versions (and in other languages that don't provide namespaces) you would have to prefix your class and method names with something to reduce the risk of a name clash. e.g. a movieyoda_parse_file function.

提交回复
热议问题