tag:blogger.com,1999:blog-8550962.post4294652889832740093..comments2024-01-27T21:10:48.750-08:00Comments on PyDev adventures: Making code work in Python 2 and 3Fabio Zadroznyhttp://www.blogger.com/profile/04202246218394712738noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-8550962.post-37125733000164502662010-03-12T10:51:20.756-08:002010-03-12T10:51:20.756-08:00Thanks for all of the tips. By the way, it would ...Thanks for all of the tips. By the way, it would be great to see more information like this to make its way to the official <a href="http://wiki.python.org/moin/PortingPythonToPy3k" rel="nofollow">Porting to Python 3</a> wiki.Andrewhttps://www.blogger.com/profile/12127985852882927242noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-61511019198697816842008-12-07T03:35:00.000-08:002008-12-07T03:35:00.000-08:00That's right... Actually, in my case, I want to su...That's right... Actually, in my case, I want to support from Python 2.1 to Python 3.0 with a single codebase.Fabio Zadroznyhttps://www.blogger.com/profile/04202246218394712738noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-49566445068088394112008-12-06T21:52:00.000-08:002008-12-06T21:52:00.000-08:00It can be noted that most of these tricks aren't n...It can be noted that most of these tricks aren't necessary under Python 2.6, so this specifically is for Python 2.5 or earlier.<BR/><BR/>Personally I would use 2to3 to support 2.5 and 3.0 at the same time, but there may be reasons why you really want code to run on both without conversion tools in some limited cases.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8550962.post-88114571159891290392008-11-28T20:51:00.000-08:002008-11-28T20:51:00.000-08:00I was thinking of your list of tips Fabio and deci...I was thinking of your list of tips Fabio and decided to fully document another 2 <I>and</I> 3 hurdle: metaclasses.<BR/><BR/>The syntax change introduced in Python 3 precludes doing an "if sys.version <" style hack, but there is a simple solution.<BR/><BR/>See: <BR/><BR/><A HREF="http://mikewatkins.ca/2008/11/29/python-2-and-3-metaclasses/" REL="nofollow">Python 2 and 3: Metaclasses</A><BR/><BR/>Maybe a 2 and 3 'best practices' FAQ somewhere might be in order.Michael Watkinshttps://www.blogger.com/profile/07422297152178282002noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-48439077672282903822008-11-27T10:02:00.000-08:002008-11-27T10:02:00.000-08:00Yes, I agree, those are the last resort -- but for...Yes, I agree, those are the last resort -- but for Pydev not doing so would complicate things the other way: <BR/><BR/>One would have to know the version of Python before choosing which script to run -- and add a considerable boilerplate for doing so -- so, back / foreporting fixes seems worse for me.<BR/><BR/>Actually, keeping separate branches for the same code is not something I usually consider a good solution for anything unless you want to keep something stuck to a given version and start applying only high-priority patches, as happens with older versions of Python -- e.g.: 2.3.xx, 2.4.xx.<BR/><BR/>Also, the 2to3 is not really 100% correct. Even after running it, there are a number of things that have to be manually patched before all things work, so, maintaining those 2 versions up to date is actually considerably more tricky than just running 2to3.<BR/><BR/>As for the print, you're right, when a single string is printed to sys.stdout that solution can be adopted (thanks for the tip).Fabio Zadroznyhttps://www.blogger.com/profile/04202246218394712738noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-74100652346776955302008-11-27T10:01:00.000-08:002008-11-27T10:01:00.000-08:00Fabio, re your point #1, its not that ``print`` ca...Fabio, re your point #1, its not that ``print`` can't be used, it just can't be used as a statement (as it was in Python < 3) because it is now a function.<BR/><BR/>print "boo" therefore becomes<BR/>print("boo")<BR/><BR/>Not very scary and relatively minor in the grand scheme of things.<BR/><BR/>@eddy: there is no need to separate the parenthesis from the print statement in 2.x versions of Python:<BR/><BR/>>>> sys.version<BR/>'2.5.2 ...'<BR/>>>> print("foo")<BR/>foo<BR/>>>> <BR/><BR/>This has been the case since 2.4x at least, perhaps much longer than that.<BR/><BR/>@Lawrence: <I>I really hope nobody will do such tricks</I><BR/><BR/>Perhaps the right answer is not being categorical about such things. I have been doing testing on one full stack web application framework and object database which supports 2.x and 3.x; by luck or design (I think the latter, and its not my code so the praise is not self centred) very few "compatibility shims" were required - nine 'if sys.version < "3"' out of 64 modules. Not bad.<BR/><BR/>That there have frequently been language and library changes over the years that require such tests in order for a package to support more than one version of 2.x, so the precedence is set and is not all bad.<BR/><BR/>If a package can achieve dual 2 and 3 compatibility with a minor shim here and there, I don't see anything untoward in carrying on with that tradition.<BR/><BR/>That said I'm sure we'll see many non trivial packages will not have such a smooth transition.Michael Watkinshttps://www.blogger.com/profile/07422297152178282002noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-55209672544884087042008-11-27T09:28:00.000-08:002008-11-27T09:28:00.000-08:00You can use "print", but only in a resticted way.p...You can use "print", but only in a resticted way.<BR/><BR/>print ("Hello World")<BR/><BR/>works in both 2.x and 3.0. The basic point is that a single string surrounded by parentheses and separated from "print" by one or more spaces will work.<BR/><BR/>See the "Backwards Compatibility" section of PEP 3105 for more details.Unknownhttps://www.blogger.com/profile/03968384459369215151noreply@blogger.comtag:blogger.com,1999:blog-8550962.post-3012791031354310562008-11-27T05:56:00.000-08:002008-11-27T05:56:00.000-08:00I really hope nobody will do such tricks to mainta...I really hope nobody will do such tricks to maintain compatibility among the two incompatible versions. <BR/><BR/>I think the best way to go is to have two different branches and back/foreporting fixes or just using 2to3 from a real Python 2.6 codebase.Anonymousnoreply@blogger.com