面试准备

值传递和参数传递

>>> 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')
  1. 魔法方法如何重写?是否需要?
  2. super函数的使用?
    https://docs.python.org/3.5/library/functions.html?highlight=super#super
  3. 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

  1. 从头开始
    • python -m pdb test.py
    • pdb.run(‘pdb_script.MyObj(5).go()’)
  2. pdb.set_trace()
  3. after failure: pdb.pm()
  4. 4.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值