值传递和参数传递
>>> a=5
>>> '%.*s' % (a,'fang jia xin')
'fang '
>>> a = 6>>> b = (a,'fang jia xin')>>> '%.*s' % b'fang j'>>> a = 7>>> b(6, 'fang jia xin')
元组的变量传递,是copy后值传递参数
不可变对象传递值,在函数作用域内的改变,不影响函数外变量的值;可变(mutable)对象参数传递,函数可直接更改变量
class Person:
name=[]
p1=Person()
p2=Person()
p1.name.append(1)
print p1.name # [1]
print p2.name # [1]
print Person.name # [1]
类变量的传递也类似函数的参数传递
class Person(object):
name=[]
def __init__(self, xing, ming):
self.xing = xing
self.ming = ming
def __getattr__(self, name):
if name == 'fang':
print('me')
- 魔法方法如何重写?是否需要?
- super函数的使用?
https://docs.python.org/3.5/library/functions.html?highlight=super#super getter、setter的使用?
迭代器和生成器
In Python 2.6 and higher, the best way to implement an iterator is generally to use the appropriate abstract base class from the collections standard library module – in Python 2.6, the code might be (remember to call the method next instead in Python 3):
import collections
class infinite23s(collections.Iterator):
def next(self): return 23
构建迭代器最好的方法,重写itertool.collections里的next方法
new & init
Use new when you need to control the creation of a new instance. Use init when you need to control initialization of a new instance.
new用来实例化一个类,init用来初始化instance
In general, you shouldn’t need to override new unless you’re subclassing an immutable type like str, int, unicode or tuple.
new的应用:singleton pattern(单例模式)
单例模式,也叫单子模式,是一种常用的软件设计模式。在应用这个模式时,单例对象的类必须保证只有一个实例存在。
@singleton
class MyClass:
#实现单例模式
- factory
decorator
super
返回值:Return a proxy object that delegates method calls to a parent or sibling class of type.
pdb
- 从头开始
- python -m pdb test.py
- pdb.run(‘pdb_script.MyObj(5).go()’)
- pdb.set_trace()
- after failure: pdb.pm()
- 4.