python面试重点

  1. webbrowser的使用

    webbrowser模块提供了一个高级接口来显示基于Web的文档(就是用浏览器登录指定的url)

    使用系统默认浏览器登录

    webbrowser.open_new("http://www.baidu.com")
    

    使用指定浏览器登录到指定url

    #指定浏览器路径
    chromepath=r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
    #将地址注册到webbrowser
    webbrowser.register('chrome',None,webbrowser.BackgroundBrowser(chromepath))
    #打开指定网址
    
    webbrowser.get('chrome').open_new("http://www.baidu.com")
    
  2. and or语句

    格式: bool and a or b
    这个bool是个表达式或者bool值,为True这条语句返回值为a,为False返回值为b

    a="first"
    b="srcond"
    x=1
    print(x>1 and a or b)
    
  3. 将一个序列倒序遍历出来

    #序列是list
    list=[1,2,3]
    for i in range(reversed(list)):
        print(i)
    #序列不是list
    for i in range(len(list)-1,-1,-1):
        print(list[i])
    
  4. complex

    创建一个复数

    print(complex(1,2))
    输出结果为1+2j
    
  5. 类的__call__函数

    一个类实例也可以变成一个可调用对象,只需要实现一个特殊方法__call__()。

    class User(object):
        def __init__(self,id):
            self.id=id
        def __call__(self,friend):
            self.friend=friend
            print("这是{}".format(self.friend))
    u1=User(1)
    u1('time')
    #输出:这是time
    

    简单点说:就是本来u1()是错误的,重写__call__方法,就可以u1()了

猜你喜欢

转载自blog.csdn.net/qq_37892223/article/details/81448086
今日推荐