python tutorial 学习笔记 (五) scope,namespace

    技术2022-05-19  18

    Namespace

    A mapping from name to objects. Names in different namespaces have no relation at all. Most namespaces are currently implemented as Python dictionaries e.g.: the set of built-in names; global names in a module; local names in a function invocation; in a sense, the attributes of an object

    Namespaces are created at different moments and have different lifetimes.

    Attribute

    Any name following a dot. modules’s attribute    vs.     global names in the module     : they are not the same, but they share the same namespace. exception is __dict__ attribute is not a global name in that module.

    Attibutes may be read-only or writable. Writable attibutes can be assigned value or be delted by “del” keyword.

    Scope

    A scope is a textual region of the Python program when a namespace is directly accessible. i.e.: don’t need dot. At least three nested scopes whose namespaces are ditrectly accessible:

    the innter most scope( the namespaces of any enclosing function); middle scope: current module’s global names outermost scope: the namespace containing built-in names.

    If a name is declared global, then all references go directly to the middle scope containing the module’s global names.  Otherwise, all variables found outside of the innermose scope are read-only.  The global scope of a function defined in a module is that module’s namespace, no matter from where or by what alias the function is called.

    **[博主按]: And there is a scope that can be imported from module. When there’s confliction between names imorted from scope, the reference will go to the last imported one.


    最新回复(0)