时间:2022-04-22 点击: 次 来源:网络 作者:佚名 - 小 + 大
零基础学python爬虫线路教程要多久零基础学python系列课程分享 下载地址 https://share.weiyun.com/S11bpehi 资料目录 跟老齐学Python从入门到精通 电子工业出版社 大家对于“变量”已经不陌生了吧,这里是第二次出现了,“一回生二回熟”。一条金科玉律是:在Python中“变量无类型,对象有类型”。变量相当于一个标签,贴在了不同的对象上。这种“贴”的动作,可以通过复制语句完成。 同样,对字符串类型的对象也是这样,能够通过赋值语句,将对象与某个标签(变量)关联起来。 >>> b = "hello,world" >>> b 'hello,world' >>> print b hello,world 依然请出type()函数,得到变量类型: >>> type(b) <type 'str'> 可能读者这时候心存疑惑,它们三者之间有区别吗?首先明确,repr()和``是一致的,就不用区别了。接下来需要区别的就是repr()和str,一个最简单的区别:repr是函数,str跟int一样是一种对象类型。不过,仅这么说是不能完全解惑的,幸亏有Google让我辈使用,你会找到很多人对这两者进行区分的内容,我推荐以下这些: 1.When should i use str()and when should i use repr()? Almost always use str when creating output for end users. repr is mainly useful for debugging and exploring.For example,if you suspect a string has non printing characters in it,or a float has a small rounding error,repr will show you;str may not. repr can also be useful for for generating literals to paste into your source code.It can also be used for persistence(with ast.literal_eval or eval),but this is rarely a good idea--if you want editable persisted values,something like JSON or YAML is much better,and if you don't plan to edit them,use pickle. 2.In which cases i can use either of them? Well,you can use them almost anywhere.You shouldn't generally use them except as described above. 3.What can str()do which repr()can't? Give you output fit for end-user consumption--not always(e.g., str(['spam','eggs'])isn't likely to be anything you want to put in a GUI),but more often than repr. 4.What can repr()do which str()can't Give you output that's useful for debugging--again,not always(the default for instances of user-created classes is rarely helpful),but whenever possible. And sometimes give you output that's a valid Python literal or other expression--but you rarely want to rely on that except for interactive exploration. |