Python知识点和总结(update@20220722)
pip使用国内源
创建$HOME/pip/pip.conf文件, 写入一下内容
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=https://pypi.tuna.tsinghua.edu.cn
关于.pyc文件
pyc 文件是python 脚本经编译后的中间文件(byte code, 与机器无关), 可以由python 命令
python -m test.py
或者模块 py_compile compileall而生成
生成器(generator)和迭代器(iterator)
就用法上而言, 生成器generator和list的区别是?
关于a, b = b, a+b
a, b = b, a+b 实际上是元组tupple的赋值, 等价于(a, b) = (b, a+b)而非a=b, b=a+b(实际上,C, C++, JAVA 都没有这样的赋值)
关于__slots__
The special attribute __slots__ allows you to explicitly state which instance attributes you expect your object instances to have, with the expected results:
- faster attribute access.
- space savings in memory.
global关键字的作用
The basic rules for global keyword in Python are:
- When we create a variable inside a function, it is local by default.
- When we define a variable outside of a function, it is global by default. You don’t have to use
globalkeyword. - We use
globalkeyword to read and write a global variable inside a function. - Use of
globalkeyword outside a function has no effect.
python multiline comment
'''
This is a multiline
comment.
'''
python multiline string
"""line 1
line 2
line 3"""
Built-in Class Attributes
| Attribute | Description |
|---|---|
__dict__ |
This is a dictionary holding the class namespace. |
__doc__ |
This gives us the class documentation if documentation is present. None otherwise. |
__name__ |
This gives us the class name. |
__module__ |
This gives us the name of the module in which the class is defined. In an interactive mode it will give us __main__. |
__bases__ |
A possibly empty tuple containing the base classes in the order of their occurrence. |
Built-in Functions
Built-in Data Types
| Text Type: | str |
|---|---|
| Numeric Types: | int, float, complex |
| Sequence Types: | list, tuple, range |
| Mapping Type: | dict |
| Set Types: | set, frozenset |
| Boolean Type: | bool |
| Binary Types: | bytes, bytearray, memoryview |
Problems
- Python如何使用C/C++代码