TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Migrating to Python 3 with pleasure

320 pointsby arogozhnikovover 7 years ago

19 comments

reover 7 years ago
I&#x27;ve been toying around with Python 3 and using it for most of my personal&#x2F;hack projects, but I somehow missed the unpacking improvements: <a href="https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0448&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0448&#x2F;</a><p>In particular, being able to create an updated copy of a dict with a single expression is pretty cool:<p><pre><code> return {**old, &#x27;foo&#x27;: &#x27;bar&#x27;} # Old way new = old.copy new[&#x27;foo&#x27;] = [&#x27;bar&#x27;] return new</code></pre>
评论 #16305990 未加载
评论 #16305857 未加载
评论 #16305878 未加载
评论 #16306400 未加载
评论 #16306923 未加载
评论 #16307054 未加载
评论 #16306285 未加载
cup-of-teaover 7 years ago
It&#x27;s so strange to me that data scientists would need to be convinced to move to Python 3. It&#x27;s superior in every way to legacy Python. I can understand maintaing Python 3 compatbility for legacy systems if you don&#x27;t want to have Python 3 as a dependancy, but data scientists will be writing mostly ad hoc code and using Jupyter notebooks. The people around me are not allowed to use Python 2, in fact they&#x27;re generally required to use the latest version of Python 3.<p>For anyone having trouble with maintaing multiple Python versions, I recommend Pyenv. You can install multiple local versions and switch between them. The selected version then uses the standard commands &quot;python&quot; and &quot;pip&quot;, etc. which you can use to make your virtualenv from.
评论 #16308389 未加载
评论 #16308996 未加载
AlexCoventryover 7 years ago
I&#x27;ve moved to python 3 over the past couple of months, after resisting for the better part of a decade. I like it.<p>One surprising thing I learned from this document is that dicts now iterate in assignment order, not hash order. That&#x27;s going to break some code for people.
评论 #16305537 未加载
评论 #16305986 未加载
评论 #16305578 未加载
评论 #16307056 未加载
评论 #16307841 未加载
评论 #16307567 未加载
makecheckover 7 years ago
I’m a little surprised at this point that Apple still doesn’t include a default Python 3.x on macOS. It’s the single thing keeping me from moving (as there’s a big difference between “just run this” and “first download this, then run this”).
评论 #16306544 未加载
评论 #16306677 未加载
评论 #16307973 未加载
评论 #16307988 未加载
评论 #16307002 未加载
评论 #16326526 未加载
amyjessover 7 years ago
I&#x27;d been a 2.7 holdout for ages, but when f-strings were greenlit for 3.6, I decided then and there that all my new personal projects would be written in 3.<p>I&#x27;m glad I did. F-strings are wonderful, as is pathlib and the enhanced unpacking syntax.<p>Since I started my current job, I&#x27;ve also been writing as many scripts as I can in Python 3 as well (and Docker has been a godsend for that because I can now deploy 3.6 scripts to the few servers we have that are still running RHEL 6).
评论 #16306826 未加载
pletnesover 7 years ago
Several posters indicate that they’ve stuck to python 2.7 even for small side projects until now. I cannot understand why? Python 3 seems to have been technically superior for a few years, and side projects must surely be good for learning something new?
评论 #16307283 未加载
eindiranover 7 years ago
If you would like to write Python 3 but need to maintain support for any particular version of Python 2, something that I can personally recommend is using the Coconut transpiler: <a href="http:&#x2F;&#x2F;coconut-lang.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;coconut-lang.org&#x2F;</a> The Coconut language is a superset of Python 3, so any valid Python 3 is valid Coconut. But the advantage of the transpiler extends beyond the language itself, in that it can target any version of Python from 2.6 onwards on the Python 2 branch and 3.2 onwards on the Python 3 branch.<p>It has been really useful for me in that I want the advantages of type annotations and f-strings and other python 3.5+ features but I have to support running in an environment with only 2.6 installed. So when I target a 3.5+ version, all of those features are maintained, but when I target 2.6, the transpiler does all the work in converting to running 2.6 code for me.
breatheoftenover 7 years ago
Didn’t know about the enforce library (<a href="https:&#x2F;&#x2F;github.com&#x2F;RussBaz&#x2F;enforce&#x2F;blob&#x2F;master&#x2F;README.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;RussBaz&#x2F;enforce&#x2F;blob&#x2F;master&#x2F;README.md</a>) — but have been wanting something like this.<p>Thanks for the useful list!
coppolaemilioover 7 years ago
the Path thing is incredible! I wasn&#x27;t aware of it. I&#x27;ll definitively be replacing my os.path.join calls for a more readable version of it.
评论 #16305510 未加载
评论 #16307060 未加载
评论 #16305430 未加载
评论 #16305569 未加载
ggmover 7 years ago
Came here to validate my own &quot;I moved&quot; experience: learned stuff I hadn&#x27;t checked on. TL;DR its never too late to learn what you can do, once you can deprecate the past.
minimaxirover 7 years ago
I&#x27;m not sure why this is targeted toward data scientists; these tips are useful for any Python user.
评论 #16305808 未加载
评论 #16306383 未加载
iandanforthover 7 years ago
Is the np.dot -&gt; @ tip reliable? I thought these were fairly different in practice.
评论 #16305645 未加载
评论 #16305606 未加载
jtchangover 7 years ago
I had so many issues trying to install python3 in an existing server that I ended up having to go back. pip kept complaining and it was just really annoying. Then some libraries were not compatible and it felt like it wasn&#x27;t worth it.
评论 #16306578 未加载
Noumenon72over 7 years ago
Is there some kind of slash operator making this statement work? Is this a way to concatenate things?<p>train_path = datasets_root &#x2F; dataset &#x2F; &#x27;train&#x27;
评论 #16306150 未加载
评论 #16306146 未加载
sdsad46464over 7 years ago
Type hinting isn&#x27;t part of Python 3, it&#x27;s part of Python 3.5+.
评论 #16307027 未加载
brett40324over 7 years ago
I realized, just today, that the secrets module is new to 3.6 after trying to pip install it. This being provided directly by the language is a game changer, IMHO.
评论 #16305892 未加载
gesmanover 7 years ago
&lt;3 python 3 :)
donarbover 7 years ago
Another feature is that the mock module is now part of the standard library, no need to install.
juloxover 7 years ago
also you can use pythonconverter.com