zoukankan      html  css  js  c++  java
  • python面试2

    转载请注明出处http://www.cnblogs.com/goodhacker/p/3387027.html

    1.python中类方法、类实例方法、静态方法有何区别?

    区别: 

    • 类方法和静态方法都可以被类和类实例调用,类实例方法仅可以被类实例调用
    • 类方法的隐含调用参数是类,而类实例方法的隐含调用参数是类的实例,静态方法没有隐含调用参数

    使用示例:

    python代码:

    复制代码
     1 class A(object):  
     2     def foo(self,x):  
     3     #类实例方法  
     4         print "executing foo(%s,%s)"%(self,x)  
     5  
     6     @classmethod  
     7     def class_foo(cls,x):  
     8     #类方法  
     9         print "executing class_foo(%s,%s)"%(cls,x)  
    10  
    11     @staticmethod  
    12     def static_foo(x):  
    13     #静态方法  
    14         print "executing static_foo(%s)"%x
    复制代码

    调用方法

    复制代码
    1 a = A()  
    2 a.foo(1)     //print   : executing foo(<__main__.A object at 0xb77d67ec>,1)
    3   
    4 a.class_foo(1)    //executing class_foo(<class '__main__.A'>,1)  
    5 A.class_foo(1)    //executing class_foo(<class '__main__.A'>,1)  
    6   
    7 a.static_foo(1)    //executing static_foo(1)  
    8 A.static_foo(1)   //executing static_foo(1)  
    复制代码

     2.python中xrange和range的异同

      xrange的用法与range完全相同,所不同的是xrange生成的不是一个list,而是一个生成器。要生成很大的数字序列的时候,用xrange会比range性能优很多,因为不需要一上来就开辟一块很大的内存空间。

      range会直接生成一个list对象:

    1 >>> a = range(0, 50)
    2 >>> print type(a)
    3 <type 'list'>
    4 >>> print a
    5 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]

       而xrange则不会直接生成一个list,而是每次调用返回其中的一个值:

    1 >>> a = xrange(0, 50)
    2 >>> print type(a)
    3 <type 'xrange'>
    4 >>> print a
    5 xrange(50)

    3.请用python实现如下的C代码问号表达式:

    int n = a>b ?(a-b):0

    要求使用最简单的方式实现。

    1 >>> n=(3>4) and (3-4) or 0
    2 >>> print n
    3 0
    4 >>> n=(3<4) and (3-4) or 0
    5 >>> print n
    6 -1

    4.python的多线程的实现机制是什么?在什么情况下使用多线程能明显提高程序效率?

    5.写出正则表达式从一个字符串中提取链接地址,如以下字符串<href=http://www.mianwww.com/html/category/it-interview/flex>Flex</a>

    需要提取的链接为“http://www.mianwww.com/html/category/it-interview/flex

    1 >>> href = re.findall(r"<a.*?href=(http://.*?)>", string)
    2 >>> print href
    3 ['http://www.mianwww.com/html/category/it-interview/flex']

    有人说后面.*?中的.和?不要,我试了下不行

    1 >>> href = re.findall(r"<a.*?href=(http://*)>", string)
    2 >>> print href
    3 []
    4 >>> href = re.findall(r"<a.*?href=(http://*?)>", string)
    5 >>> print href
    6 []

    6.反转由单词和不定个数空格组成的字符串,要求单词中的字母顺序不变。如:"I am   a      boy"反转成“boy      a   am I”。

    1 >>> import re
    2 >>> string = "I am   a      boy"
    3 >>> revwords = ''.join(re.split(r'(s+)', string)[::-1])
    4 >>> print revwords
    5 boy      a   am I
    学习记录,小白一枚
  • 相关阅读:
    设计模式(十六):职责链模式
    设计模式(十五):状态模式
    设计模式(十四):命令模式
    设计模式(十三):模板模式
    设计模式(十二):观察者模式
    远程连接数据库常出现的错误解析
    [解决] Error Code: 1044. Access denied for user 'root'@'%' to database
    linux常用命令
    linux上svn项目管理,同步服务器,用户管理
    linux 磁盘分区
  • 原文地址:https://www.cnblogs.com/wangsirde0428/p/14590738.html
Copyright © 2011-2022 走看看