• python的namespace的理解


     
    python中的名称空间是名称(标识符)到对象的映射。
    具体来说,python为模块、函数、类、对象保存一个字典(__dict__),里面就是重名称到对象的映射。
    -------------------------------------------------------------------------------------------
    import urllib
    import re
    x=1 # 变量
    def abc(): # 函数
    pass
    def qq(self): # 方法
    pass
    class typ(object): # 类
    """docstring for typ"""
    def __init__(self, arg):
    super(typ, self).__init__()
    self.arg = arg
    def classqq(self): # 不存在于全局变量中
    pass
     
    print(globals().keys()) # 打印字典中的key值
    print()
    print(globals()) # 打印全局变量,打印出来是以字典的形式展示
    -----------------------------------------------------------------------------------------------
    dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__', '__cached__', 'urllib', 're', 'x', 'abc', 'qq', 'typ'])
     
    {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000000001DEC048>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:\Learn\practice\case1.py', '__cached__': None, 'urllib': <module 'urllib' from 'D:\Programs\Python\Python36\lib\urllib\__init__.py'>, 're': <module 're' from 'D:\Programs\Python\Python36\lib\re.py'>, 'x': 1, 'abc': <function abc at 0x00000000001F2E18>, 'qq': <function qq at 0x00000000021FAAE8>, 'typ': <class '__main__.typ'>}
    -------------------------------------------------------------------------------------------
     
     
    x=1 # 变量
    def abc(): # 函数
    pass
    def qq(self): # 方法
    pass
    class Typ(object): # 类
    """docstring for typ"""
    k=1 # 私有变量没有被init初始化
    def __init__(self):
    super(Typ, self).__init__()
    self.y = 2
    self.z = 3
    def func(self): # 函数方法不存在于全局命名空间中
    print("abcd") # 函数方法会默认return None
    func.fx = 2
     
    test1 = Typ()
    print(Typ.__dict__)
    print(test1.__dict__)
    print(test1.func.__dict__)
    print(globals().keys())
    ----------------------------------------------------------------------------------------------
    {'__module__': '__main__', '__doc__': 'docstring for typ', 'k': 1, '__init__': <function Typ.__init__ at 0x00000000029007B8>, 'func': <function Typ.func at 0x0000000002900840>, '__dict__': <attribute '__dict__' of 'Typ' objects>, '__weakref__': <attribute '__weakref__' of 'Typ' objects>}
    {'y': 2, 'z': 3}
    {'fx': 2}
    dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__', '__cached__', 'urllib', 're', 'x', 'abc', 'qq', 'Typ', 'test1'])
    [Finished in 0.1s]
     
    locals
    内置函数locals(), 返回当前函数(方法)的局部命名空间
    def function(a=1):
    b=2
    print(locals())
    return a+b
    print(function())
    -----------------------------
    {'b': 2, 'a': 1}
    3
    globals
    内置函数globals(),返回当前module的命名空间
    def function(a=1):
    b=2
    print(locals())
    return a+b
    print(function())
    print(globals().keys())
    --------------------------------------
    {'b': 2, 'a': 1}
    3
    dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__', '__cached__', 'urllib', 're', 'function'])
    [Finished in 0.1s]
     
    locals()和globals()有一个区别是,locals只读,globals可以写
     
    from module import 和 import module
    • 使用import module时,module本身被引入,但是保存它原有的命名空间,所以我们需要使用module.name这种方式访问它的 函数和变量。
    • from module import这种方式,是将其它模块的函数或者变量引到当前的命名空间中,所以就不需要使用module.name这种方式访问其它的模块的方法了。
     
     
  • 相关阅读:
    第12章 项目采购管理
    C# 利用xml动态生成带图标菜单
    C#正则表达式整理备忘
    IE8"开发人员工具"使用详解下
    拖盘控件notifyIcon演示例程
    多列选择框控件checkedListBox演示程序
    树形框treeView演示程序
    错误提示控件errorProvider演示例程
    IE8“开发人员工具”使用详解上
    c#中分割字符串的几种方法
  • 原文地址:https://www.cnblogs.com/TomBombadil/p/10979588.html
Copyright © 2020-2023  润新知