python2和python3差异对比

1. 获取用户输入
python2使用函数raw_input()
python3使用函数input()
2. 定义类
python2定义类需要在括号里写上object而python3不需要:

class  ClassName (object):
    ...

python3定义类:

class  ClassName ():
    ...

3. 子类继承
python2子类继承如下:

#父类
Class Parent(object):
    def __init__(self,attribute):

#子类
Class Child(Parent):
    def __init__(self,attribute):
        super(Child, self).__init__(attribute)
  • 在Python2中使用继承时,必需在定义父类时在括号内指定 object
  • 函数 super() 需要两个实参:子类名和对象 self

python3子类继承如下:

#父类
Class Parent():
    def __init__(self,attribute):

#子类
Class Child(Parent):
    def __init__(self,attribute):
        super().__init__(attribute)

4. urllib库的使用
Python 2.x 里的 urllib2 库在 Python 3.x 里,urllib2 改名为 urllib,并被分成一些子模块: urllib.request 、urllib.parse 和 urllib.error 。

python2.x使用 urllib实例:

from urllib import urlopen  
html = urlopen("http://pythonscraping.com/pages/page1.html")
print(html.read())

python3.x使用 urllib实例:

from urllib.reque st  import u rlopen
html = urlopen("http://pythonscraping.com/pages/page1.html")
print(html.read())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值