95 lines
2.6 KiB
Markdown
95 lines
2.6 KiB
Markdown
|
### type 和 isinstance 区别
|
|||
|
|
|||
|
- type()不会认为子类是一种父类类型。
|
|||
|
- isinstance()会认为子类是一种父类类型。
|
|||
|
|
|||
|
```python
|
|||
|
>>> class A:
|
|||
|
... pass
|
|||
|
...
|
|||
|
>>> class B(A):
|
|||
|
... pass
|
|||
|
...
|
|||
|
>>> isinstance(A(), A)
|
|||
|
True
|
|||
|
>>> type(A()) == A
|
|||
|
True
|
|||
|
>>> isinstance(B(), A)
|
|||
|
True
|
|||
|
>>> type(B()) == A
|
|||
|
False
|
|||
|
```
|
|||
|
|
|||
|
### 删除变量定义
|
|||
|
|
|||
|
- del
|
|||
|
- 通过使用 del 语句删除单个或多个对象
|
|||
|
|
|||
|
```python
|
|||
|
>>> a = 1
|
|||
|
>>> a
|
|||
|
1
|
|||
|
>>> del a
|
|||
|
>>> a
|
|||
|
Traceback (most recent call last):
|
|||
|
File "<stdin>", line 1, in <module>
|
|||
|
NameError: name 'a' is not defined
|
|||
|
>>>
|
|||
|
```
|
|||
|
|
|||
|
### 模块 `__name__`
|
|||
|
|
|||
|
- 区分是自身运行还是被作为模块导入
|
|||
|
|
|||
|
```python
|
|||
|
#!/usr/bin/python3
|
|||
|
# Filename: using_name.py
|
|||
|
|
|||
|
if __name__ == '__main__':
|
|||
|
print('程序自身在运行')
|
|||
|
else:
|
|||
|
print('我来自另一模块')
|
|||
|
|
|||
|
```
|
|||
|
|
|||
|
|
|||
|
|
|||
|
### dir 函数
|
|||
|
|
|||
|
- 内置的函数 dir() 可以找到模块内定义的所有名称。以一个字符串列表的形式返回
|
|||
|
|
|||
|
```python
|
|||
|
>>> import module_support
|
|||
|
>>> dir(module_support)
|
|||
|
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'add']
|
|||
|
>>>
|
|||
|
>>> import sys
|
|||
|
>>> dir(sys)
|
|||
|
['__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__',
|
|||
|
'__package__', '__stderr__', '__stdin__', '__stdout__',
|
|||
|
'_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe',
|
|||
|
'_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv',
|
|||
|
'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder',
|
|||
|
'call_tracing', 'callstats', 'copyright', 'displayhook',
|
|||
|
'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix',
|
|||
|
'executable', 'exit', 'flags', 'float_info', 'float_repr_style',
|
|||
|
'getcheckinterval', 'getdefaultencoding', 'getdlopenflags',
|
|||
|
'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit',
|
|||
|
'getrefcount', 'getsizeof', 'getswitchinterval', 'gettotalrefcount',
|
|||
|
'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info',
|
|||
|
'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path',
|
|||
|
'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1',
|
|||
|
'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit',
|
|||
|
'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout',
|
|||
|
'thread_info', 'version', 'version_info', 'warnoptions']
|
|||
|
```
|
|||
|
|
|||
|
- 如果没有给定参数,那么 dir() 函数会罗列出当前定义的所有名称
|
|||
|
|
|||
|
```python
|
|||
|
>>> dir()
|
|||
|
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'answers', 'b', 'c', 'i', 'keyword', 'module_support', 'name', 'questions', 'random', 'str', 'student', 'tup', 'w', 'x', 'y']
|
|||
|
>>>
|
|||
|
```
|
|||
|
|