西维蜀黍

【Python】常见错误

Variable

未定义的 variable

>>> len(abc)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'abc' is not defined

Dict

不存在的 Key

>>> a = {}
>>> a["s"]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
KeyError: 's'

Function

未定义的函数

>>> fun()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'fun' is not defined
>>> a = None
>>> a()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'NoneType' object is not callable
  ...


【MySQL】Lock Demo(锁实验)- Read

Locking Read

If you query data and then insert or update related data within the same transaction, the regular SELECT statement does not give enough protection. Other transactions can update or delete the same rows you just queried. InnoDB supports two types of locking reads that offer extra safety:

  1. select ... for share
  2. select ... for update

It has the potential to produce a deadlock, depending on the isolation level of the transaction. The opposite of a non-locking read.

  ...


【Python】Basics - 特殊变量(Special Variables)

  ...


【Python】魔术方法(Magic Methods)

Python的魔术方法一般以__methodname__的形式命名,如:__init__(构造方法)、 __getitem____setitem____delitem__(调用del obj[key]时对应触发的函数)、 __len__(对类实例调用len(…)时对应触发的函数)等。

魔术方法具体有:

  • __init__(self):构造方法
  • __del__ :析构函数,释放对象时使用
  • __getitem__(self,key):返回键对应的值(按照索引获取值)
  • __setitem__(self,key,value):设置给定键的值(按照索引赋值)
  • __delitem__(self,key):删除给定键对应的元素
  • __len__():对类实例调用len(…)时对应触发的函数
  • __cmp_: 比较运算
  • __call_:调用
  • __add__:加运算
  • __sub__:减运算
  • __mul__:乘运算
  • __div__:除运算
  • __mod__:求余运算
  • __pow__:幂

需要注意的是,这些成员里面有些是方法,调用时要加括号,有些是属性,调用时不需要加括号。

  ...


【Python】import 问题排查

解决思路

1

使用pip list查看本项目或者系统中已经安装了未找到的模块,这里具体又分为使用了 virenv 和未使用的情况。

2

直接在 terminal 中进入到当前工作文件夹(cd <your target folder>),测试以下 import:

$ python
Python 2.7.17 (default, Oct 24 2019, 12:57:47)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.8)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>import <your target module name>
  ...