python 面试题 - 知识点整理


1. 在判断object是否是class的instances时,type和isinstance函数的区别?

type(obj) => <type 'instance'>

type(cls) => <type 'classobj'>

由上可知,所有obj type后统一为 instance type; 而cls type后统一为classobj type

isinstance(obj,class),如果object是class的instance,返回True。


2. 通过重写内建函数,实现文件open之前检查文件格式?

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4

def open(filename,mode):
    import __builtin__
    
    file = __builtin__.open(filename,mode)
    
    if file.read(5) not in("GIF87", "GIF89"): 
        raise IOError, "not aGIF file"
    file.seek(0) 
    return file
    
fp = open("sample/test.gif","r")
print len(fp.read()), "bytes"

3. 重新实现str.strip(),注意不能使用string.*strip()

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4


# TODO rstrip
def rightStr(string,split=' '):
    endind = string.rfind(split)
    res = string
    while endind != -1 and endind == len(res)-1:
        res = res[:endind]
        endind = res.rfind(split)
    
    return res


# TODO lstrip
def leftStr(string,split=' '):
    startind = string.find(split)
    res = string
    while start
  • 2
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值