|
| 1 | +# 第24章 python命名空间 |
| 2 | + |
| 3 | +<!-- TOC --> |
| 4 | + |
| 5 | +- [第24章 python命名空间](#%e7%ac%ac24%e7%ab%a0-python%e5%91%bd%e5%90%8d%e7%a9%ba%e9%97%b4) |
| 6 | + - [内置命令空间](#%e5%86%85%e7%bd%ae%e5%91%bd%e4%bb%a4%e7%a9%ba%e9%97%b4) |
| 7 | + - [全局命名空间](#%e5%85%a8%e5%b1%80%e5%91%bd%e5%90%8d%e7%a9%ba%e9%97%b4) |
| 8 | + - [局部命名空间](#%e5%b1%80%e9%83%a8%e5%91%bd%e5%90%8d%e7%a9%ba%e9%97%b4) |
| 9 | + - [区别](#%e5%8c%ba%e5%88%ab) |
| 10 | + - [func函数内存地址](#func%e5%87%bd%e6%95%b0%e5%86%85%e5%ad%98%e5%9c%b0%e5%9d%80) |
| 11 | + - [NotImplementedError](#notimplementederror) |
| 12 | + |
| 13 | +<!-- /TOC --> |
| 14 | + |
| 15 | +## 内置命令空间 |
| 16 | + |
| 17 | +就是python解释器 ,启动就可以使用的名字存储在内置命名空间中 |
| 18 | +内置的名字在启动解释器的时候被加载进内存里 |
| 19 | + |
| 20 | + |
| 21 | +## 全局命名空间 |
| 22 | + |
| 23 | +自己写的代码,但不是函数中的代码 |
| 24 | + |
| 25 | +是在程序从上到下被执行的过程中依次加载进内存的 |
| 26 | +放置了我们设置的所有变量名和函数名 |
| 27 | + |
| 28 | + |
| 29 | +## 局部命名空间 |
| 30 | + |
| 31 | +就是函数内部定义的名字 |
| 32 | +当调用函数的时候,才会产生这个名称空间,随着函数执行的结束,这个命名空间就消失了 |
| 33 | + |
| 34 | + |
| 35 | +## 区别 |
| 36 | + |
| 37 | +1、在局部: 可以使用全局,内置命名空间中的名字 |
| 38 | + |
| 39 | +2、在全局:可以使用内置命名空间中的名字,但是不能使用局部中变量使用 |
| 40 | + |
| 41 | +例子: |
| 42 | +```python |
| 43 | +def func(): |
| 44 | + a = 1 |
| 45 | + |
| 46 | + |
| 47 | +func() |
| 48 | +print(a) |
| 49 | +``` |
| 50 | + |
| 51 | +运行结果: |
| 52 | + |
| 53 | +``` |
| 54 | +NameError: name 'a' is not defined |
| 55 | +``` |
| 56 | + |
| 57 | +3、在内置: 不能使用局部和全局的名字的 |
| 58 | + |
| 59 | +顺序是这样的: |
| 60 | +**`内置>全局>局部`** |
| 61 | + |
| 62 | +例子: |
| 63 | +```python |
| 64 | +def max(): |
| 65 | + print("in max func") |
| 66 | + |
| 67 | + |
| 68 | +max() |
| 69 | +``` |
| 70 | + |
| 71 | +运行结果: |
| 72 | +```python |
| 73 | +in max func |
| 74 | +``` |
| 75 | + |
| 76 | +在正常情况下,直接使用内置的名字 |
| 77 | +当我们在全局定义了和内置名字空间中同名的名字时,就会使用全局的名字 |
| 78 | +一级一级找 |
| 79 | + |
| 80 | + |
| 81 | +## func函数内存地址 |
| 82 | + |
| 83 | +```python |
| 84 | +# 函数名() 函数的调用 |
| 85 | +# 加入id 就是函数的内存地址 |
| 86 | + |
| 87 | + |
| 88 | +def max(): |
| 89 | + print("in max func") |
| 90 | + |
| 91 | + |
| 92 | +print(max) |
| 93 | +print(id(max)) |
| 94 | +``` |
| 95 | +运行结果: |
| 96 | +``` |
| 97 | +<function max at 0x0000025D7091D9D8> |
| 98 | +2600343820760 |
| 99 | +
|
| 100 | +``` |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +## NotImplementedError |
| 105 | + |
| 106 | +Python编程中raise可以实现报出错误的功能,而报错的条件可以由程序员自己去定制。 |
| 107 | + |
| 108 | +在面向对象编程中,可以先预留一个方法接口不实现,在其子类中实现。 |
| 109 | + |
| 110 | +如果要求其子类一定要实现,不实现的时候会导致问题,那么采用raise的方式就很好。 |
| 111 | + |
| 112 | +而此时产生的问题分类是NotImplementedError |
| 113 | + |
| 114 | + |
| 115 | +例子: |
| 116 | +```python |
| 117 | +class ClassDemo(object): |
| 118 | + def run(self): |
| 119 | + raise NotImplementedError |
| 120 | + |
| 121 | + |
| 122 | +class ChildClass(ClassDemo): |
| 123 | + def run(self): |
| 124 | + print("Hello world") |
| 125 | + |
| 126 | + |
| 127 | +ChildClass().run() |
| 128 | +``` |
| 129 | +<br> |
| 130 | + |
| 131 | +例子: |
| 132 | +```python |
| 133 | +class ClassDemo(object): |
| 134 | + def run(self): |
| 135 | + raise NotImplementedError |
| 136 | + |
| 137 | + def wrong(self): |
| 138 | + # Will raise a TypeError |
| 139 | + NotImplemented = "don't do this" |
| 140 | + return NotImplemented |
| 141 | + |
| 142 | + |
| 143 | +class ChildClass(ClassDemo): |
| 144 | + def run(self): |
| 145 | + print("Hello world") |
| 146 | + |
| 147 | + def wrong(self): |
| 148 | + print("wrong") |
| 149 | + |
| 150 | + |
| 151 | +ChildClass().run() # Hello world |
| 152 | + |
| 153 | +wrong = ClassDemo().wrong() |
| 154 | +print(wrong) # don't do this |
| 155 | + |
| 156 | +``` |
| 157 | + |
| 158 | + |
| 159 | +这里区分下 NotImplemented && NotImplementedError |
| 160 | + |
| 161 | +``` |
| 162 | + type(NotImplemented) |
| 163 | +<class 'NotImplementedType'> |
| 164 | + type(NotImplementedError) |
| 165 | +<class 'type'> |
| 166 | +issubclass(NotImplementedError,Exception) |
| 167 | +True |
| 168 | +``` |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +NotImplemented 是 Python 内建命名空间内仅有的 6 个常量(Python 中没有真正的常量)之一, |
| 173 | +其它几个分别是 False、True、None、Ellipsis 和` __debug__`。 |
| 174 | + |
| 175 | +和 Ellipsis 一样,NotImplemented 也可以被重新赋值: |
| 176 | +NotImplemented = "don't do this" |
| 177 | + |
| 178 | +两者是什么关系呢?答案是**“没啥关系”**。 |
| 179 | + |
| 180 | +Python 中 NotImplemented 广泛应用于二元魔术方法中,比如 `__eq__()、__lt__() `等等,表示该类型无法和其它类型进行对应的二元运算 |
| 181 | + |
| 182 | + |
| 183 | +例子: |
| 184 | +```python |
| 185 | +class A(object): |
| 186 | + def __init__(self, value): |
| 187 | + self.value = value |
| 188 | + |
| 189 | + def __eq__(self, other): |
| 190 | + if isinstance(other, A): |
| 191 | + print('Comparing an A with an A') |
| 192 | + return other.value == self.value |
| 193 | + if isinstance(other, B): |
| 194 | + print('Comparing an A with a B') |
| 195 | + return other.value == self.value |
| 196 | + print('Could not compare A with the other class') |
| 197 | + return NotImplemented |
| 198 | + |
| 199 | + |
| 200 | +class B(object): |
| 201 | + def __init__(self, value): |
| 202 | + self.value = value |
| 203 | + |
| 204 | + def __eq__(self, other): |
| 205 | + # raise NotImplementedError |
| 206 | + if isinstance(other, B): |
| 207 | + print('Comparing a B with another B') |
| 208 | + return other.value == self.value |
| 209 | + print('Could not compare B with the other class') |
| 210 | + return NotImplemented |
| 211 | + |
| 212 | + |
| 213 | +a, b = A(1), B(1) |
| 214 | +aa, bb = A(1), B(1) |
| 215 | +a == aa # True |
| 216 | +b == bb # True |
| 217 | +a == b # True |
| 218 | +b == a # True |
| 219 | + |
| 220 | +``` |
| 221 | + |
| 222 | +运行结果: |
| 223 | +``` |
| 224 | +Comparing an A with an A |
| 225 | +Comparing a B with another B |
| 226 | +Comparing an A with a B |
| 227 | +``` |
| 228 | + |
| 229 | + |
| 230 | + |
| 231 | +说明 == 运算符执行时会先寻找 B 的 `__eq__() `方法, |
| 232 | +遇到 NotImplemented 返回值则反过来去寻找 A 的 `__eq__()` 方法。 |
| 233 | + |
| 234 | + |
| 235 | +什么时候该使用 NotImplementedError? |
| 236 | + |
| 237 | +>NotImplementedError 是 RuntimeError 的子类: |
| 238 | +>issubclass(NotImplementedError, RuntimeError) # True |
| 239 | +
|
| 240 | +>官网 的建议是当你需要一个方法必须覆盖才能使用时,其效果类似于 Java 中的接口,用于定义一个未实现的抽象方法。 |
0 commit comments