python Memo

list&tuple 运算

乘以constant

>>> x = ((1,2),)
>>> x*2
((1, 2), (1, 2))
>>> x = ((1,2))
>>> x*2
(1, 2, 1, 2)
>>> 
View Code

从上面可以看出,tuple或者list和一个常数相乘,会复制元素得到一个新的tuple或list,需要注意的是有无逗号,这将决定是复制元素还是 "子tuple"。

tuple或list相加

>>> a = [1,2,3,4,5]
>>> a + [6]
[1, 2, 3, 4, 5, 6]
>>> a = (1,2,3,4,5)
>>> a +(6)

Traceback (most recent call last):
  File "<pyshell#189>", line 1, in <module>
    a +(6)
TypeError: can only concatenate tuple (not "int") to tuple
>>> a + (6,)
(1, 2, 3, 4, 5, 6)
>>> type((6))
<type 'int'>
>>> type([6])
<type 'list'>
>>> 
View Code

  tuple和list在此处略有区别

Class 类的处理

在Python中子类继承父类的过程中,如果子类不覆盖父类的__init__()方法,则子类默认将执行与父类一样的初始化方法。但是假如子类自己重写了(也成为覆盖)父类的__init__()方法,那么就需要显式的调用父类的初始化方法了。有两种方法可以做到:

     1:ParentClass.__init__(),父类名加上init函数

     2:super(type,cls).__init__() 

      重点介绍下这个,这个也是Python在借鉴了C++和JAVA的经验后,做出的改进语言熟悉程度的一种努力。

     super(type,cls)实质上是super类的3个静态方法之一。

原文地址:https://www.cnblogs.com/vin-yuan/p/5072661.html