<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>python3 &#8211; 天地一沙鸥</title>
	<atom:link href="https://haoluobo.com/tag/python3/feed/" rel="self" type="application/rss+xml" />
	<link>https://haoluobo.com</link>
	<description>to be continue....</description>
	<lastBuildDate>Thu, 16 Dec 2021 03:30:04 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.2</generator>
	<item>
		<title>Python 3的type hints</title>
		<link>https://haoluobo.com/2017/01/python-3-type-hints/</link>
		
		<dc:creator><![CDATA[vicalloy]]></dc:creator>
		<pubDate>Sat, 14 Jan 2017 07:10:52 +0000</pubDate>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python3]]></category>
		<guid isPermaLink="false">/?p=11474</guid>

					<description><![CDATA[近年来新出的语言Go、Rust、Swift都无疑例外的是静态类型。随着软件的越来越复杂，动态语言“太过随意”的 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>近年来新出的语言Go、Rust、Swift都无疑例外的是静态类型。随着软件的越来越复杂，动态语言“太过随意”的缺点也越来越明显。此外随着IDE的发展，静态类型语言“繁琐”的缺点也得到了很好的规避。<br>在学习Swift的时候，Swift的类型推导给了留下了很深的印象。类型推导或许是现阶段兼顾类型检查以及动态语言简洁的最佳解决方案。<br>Python从3.5开始加入了type hints（类型注释）。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
def greeting(name: str) -&gt; str:
    return &#039;Hello &#039; + name
</pre></div>


<p>从名字“type hints”就可以看出，只是类型注释并不是强制要求。由于只是“注释”，“type hints”需要第三方工具的配合。你需要使用<a href="https://github.com/python/mypy">Mypy</a>来对代码进行语法检查。另外PyCharm在新版本中也加入了对“type hints”的支持。</p>



<p>对我而言“type hints”无疑是一个非常棒的特性，让Python也具备了类似Swift的类型推导功能。希望日后的各类Python都能加入type hints的支持。另外就希望各类开发工具可以充分的利用type hints特性。</p>



<p>注：<br>Swift中使用“?”表示option，Python里则需要写成Optional[str]相比之下有些太过繁琐。</p>



<ul class="wp-block-list"><li>相关链接
<ul>
<li><a href="https://github.com/python/mypy">Mypy</a></li>
<li><a href="https://docs.python.org/3/library/typing.html">typing — Support for type hints</a></li>
</ul>
</li></ul>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>让你的python程序同时兼容python2和python3</title>
		<link>https://haoluobo.com/2013/01/python2and3/</link>
		
		<dc:creator><![CDATA[vicalloy]]></dc:creator>
		<pubDate>Tue, 08 Jan 2013 06:55:24 +0000</pubDate>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python3]]></category>
		<guid isPermaLink="false">/?p=10867</guid>

					<description><![CDATA[python邮件列表里有人发表言论说“python3在10内都无法普及”。在我看来这样的观点有些过于悲观，py [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>python邮件列表里有人发表言论说“python3在10内都无法普及”。在我看来这样的观点有些过于悲观，python3和python2虽然不兼容，但他们之间差别并没很多人想像的那么大。你只需要对自己的代码稍微做些修改就可以很好的同时支持python2和python3的。下面我将简要的介绍一下如何让自己的python代码如何同时支持python2和python3。</p>



<ul class="wp-block-list"><li><strong>放弃python 2.6之前的python版本</strong><br>python 2.6之前的python版本缺少一些新特性，会给你的迁移工作带来不少麻烦。如果不是迫不得已还是放弃对之前版本的支持吧。</li><li><strong>使用 2to3 工具对代码检查</strong><br>2to3是python自带的一个代码转换工具，可以将python2的代码自动转换为python3的代码。当然，不幸的是转换出的代码并没有对python2的兼容做任何的处理。所以我们并不真正使用2to3转换出的代码。执行 <em>2to3 t.py</em> 查看输出信息，并修正相关问题。</li><li><strong>使用python -3执行python程序</strong><br>2to3 可以检查出很多python2＆3的兼容性问题，但也有很多问题是2to3发现不了的。在加上 <em>-3</em> 参数后，程序在运行时会在控制台上将python2和python3不一致，同时2to3无法处理的问题提示出来。比如python3和python2中对除法的处理规则做过改变。使用-3参数执行4/2将提示 DeprecationWarning: classic int division 。</li><li><strong><code>from __future__ import</code></strong><br>“<code>from __future__ import</code>”后即可使使用python的未来特性了。python的完整future特性可见 <a href="http://docs.python.org/2/library/__future__.html"><code>__future__</code></a> 。python3中所有字符都变成了unicode。在python2中unicode字符在定义时需要在字符前面加 <em>u</em>，但在3中则不需要家u，而且在加u后程序会无法编译通过。为了解决该问题可以 “from <strong>future</strong> import unicode_literals” ，这样python2中字符的行为将和python3中保持一致，python2中定义普通字符将自动识别为unicode。</li><li><strong>import问题</strong><br>python3中“少”了很多python2的包，在大多情况下这些包之是改了个名字而已。我们可以在import的时候对这些问题进行处理。</li></ul>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
try:#python2
    from UserDict import UserDict
    #建议按照python3的名字进行import
    from UserDict import DictMixin as MutableMapping
except ImportError:#python3
    from collections import UserDict
    from collections import MutableMapping
</pre></div>


<ul class="wp-block-list"><li><strong>使用python3的方式写程序</strong><br>python2中print是关键字，到了python3中print变成了函数。事实上在python2.6中已经带了print函数，所以对print你直接按照2to3中给出的提示改为新写法即可。在python3中对异常的处理做了些变化，这个和print类似，直接按照2to3中的提示修改即可。</li><li><strong>检查当前运行的python版本</strong><br>有时候你或许必须为python2和python3写不同的代码，你可以用下面的代码检查当前系统的python版本。</li></ul>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
import sys
if sys.version &gt; &#039;3&#039;:
    PY3 = True
else:
    PY3 = False
</pre></div>


<ul class="wp-block-list"><li><strong>six</strong><br><a href="http://packages.python.org/six/">six</a> 提供了一些简单的工具用来封装 Python 2 和 Python 3 之间的差异性。我并不太推荐使用six。如果不需要支持python2.6之前的python版本，即使不用six也是比较容易处理兼容性问题的。使用six会让你的代码更像python2而不是python3。</li></ul>



<p>python3的普及需要每位pythoner的推动，或许你还无法立即升级到python3，但请现在就开始写兼容python3的代码，并在条件成熟时升级到python3。<br><strong>注：</strong></p>



<ul class="wp-block-list"><li><a href="http://python3porting.com/differences.html">python2同python3的差异</a></li><li>如果你更全面的了解从python2迁移到python3的相关问题，推荐阅读 <a href="http://python3porting.com/">Porting to Python 3</a> 这是一本免费的python读物。</li><li>python3的性能问题一直都让很多pythoner很纠结，不过最新的python3.3应当已经有不错的性能了。参考：<a href="http://mail.python.org/pipermail/python-dev/2012-October/121919.html">Benchmarking Python 3.3 against Python 2.7</a></li></ul>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
