What are Python namespaces all about

前端 未结 6 1248
抹茶落季
抹茶落季 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:06

    I complete the answer of S.Lott.

    I would say, namespace is a way to implement name management inside a scope, because a scope does more than name management.

    In C the scopes are of 4 types: global, function, block and function-parameters(prototype). Each of these kinds can create one or more namespaces, depending on the needs. There are 4 ns in C -- tags for s/u/e -- ids for typenames, function names and var names -- parameters inside function prototype -- members and bitfields inside s/u.

    Like that, tag identifiers and function names do not collide, but typenames defined by typedef can collide with variable names.

    In python there is a builtin namespace that encloses the global ns, and the global ns is provided by the loaded module. The builtin ns contain variables. A symbol for a variable can define an object or a function -- for example, + is defined there. The module's global ns lasts up to the termination.

    See also that and of course that.

提交回复
热议问题