{"id":329482,"date":"2023-10-02T14:00:00","date_gmt":"2023-10-02T14:00:00","guid":{"rendered":"https:\/\/realpython.com\/python312-new-features\/"},"modified":"2023-10-02T14:00:00","modified_gmt":"2023-10-02T14:00:00","slug":"python-3-12-cool-new-features-for-you-to-try","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/10\/02\/python-3-12-cool-new-features-for-you-to-try\/","title":{"rendered":"Python 3.12: Cool New Features for You to Try"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<div>\n<p><a href=\"https:\/\/www.python.org\/downloads\/release\/python-3120\/\">Python 3.12<\/a> was published on <a href=\"https:\/\/peps.python.org\/pep-0693\/\">October 2, 2023<\/a>. As usual, the new version comes out in October after lots of effort by volunteers worldwide.<\/p>\n<p>The new version comes with several new features and improvements that you\u2019ll explore in this tutorial. You can also dive into the <a href=\"https:\/\/docs.python.org\/3.12\/whatsnew\/3.12.html\">documentation<\/a> to see a complete list of all changes.<\/p>\n<p><strong>In this tutorial, you\u2019ll learn about new features and improvements, like:<\/strong><\/p>\n<ul>\n<li>Better <strong>error messages<\/strong> with helpful suggestions and guidance<\/li>\n<li>More expressive <strong>f-strings<\/strong> that are backed by Python\u2019s PEG parser<\/li>\n<li>Optimizations, including <strong>inlined comprehensions<\/strong>, for a faster Python<\/li>\n<li>A new syntax for <strong>type variables<\/strong> that you use to annotate generics<\/li>\n<li>Support for the powerful <strong><code>perf<\/code> profiler<\/strong> on Linux<\/li>\n<\/ul>\n<p>If you want to try any of the examples in this tutorial, then you\u2019ll need to use Python 3.12. The tutorials <a href=\"https:\/\/realpython.com\/installing-python\/\">Python 3 Installation &amp; Setup Guide<\/a> and <a href=\"https:\/\/realpython.com\/python-pre-release\/\">How Can You Install a Pre-Release Version of Python?<\/a> walk you through several options for adding a new version of Python to your system.<\/p>\n<p>In addition to learning more about the new features coming to the language, you\u2019ll also get some <a href=\"https:\/\/realpython.com\/python312-new-features\/#so-should-you-upgrade-to-python-312\">advice<\/a> about what to consider before upgrading to the new version. Click the link below to download code examples demonstrating the new capabilities of Python 3.12:<\/p>\n<div class=\"alert alert-warning\" role=\"alert\">\n<p><strong markdown=\"1\">Free Bonus:<\/strong> <a href=\"https:\/\/realpython.com\/bonus\/python-312-code\/\" class=\"alert-link\" data-toggle=\"modal\" data- data-focus=\"false\" markdown=\"1\">Click here to download your sample code<\/a> for a sneak peek at Python 3.12, coming in October 2023.<\/p>\n<\/div>\n<h2 id=\"improved-error-messages\">Improved Error Messages<a class=\"headerlink\" href=\"https:\/\/realpython.com\/python312-new-features\/#improved-error-messages\" title=\"Permanent link\"><\/a><\/h2>\n<p>Python is usually recognized as a good beginner language, and it\u2019s lauded for its readable syntax. One area where it\u2019s become even more user-friendly lately is <strong>error messages<\/strong>.<\/p>\n<p>In <a href=\"https:\/\/realpython.com\/python310-new-features\/\">Python 3.10<\/a>, many error messages\u2014especially for syntax errors\u2014got <a href=\"https:\/\/realpython.com\/python310-new-features\/#better-error-messages\">more informative and precise<\/a>. Similarly, <a href=\"https:\/\/realpython.com\/python311-new-features\/\">Python 3.11<\/a> added <a href=\"https:\/\/realpython.com\/python311-error-messages\/\">more information in tracebacks<\/a>, making it more convenient to pinpoint offending code.<\/p>\n<p>The latest version of Python continues the work of improving your developer experience by providing better error messages. In particular, several common error messages now come with helpful suggestions. In the rest of this section, you\u2019ll explore the new and improved messages.<\/p>\n<p>Several of the improvements relate to importing modules. In the next three examples, you try to work with \u03c0 by importing <code>pi<\/code> from <code>math<\/code>. In each example, you\u2019ll see one of the new suggestions in Python 3.12. Here\u2019s the first one:<\/p>\n<div class=\"highlight python repl\"><span class=\"repl-toggle\" title=\"Toggle REPL prompts and output\">&gt;&gt;&gt;<\/span><\/p>\n<pre><span><\/span><code><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">math<\/span><span class=\"o\">.<\/span><span class=\"n\">pi<\/span>\n<span class=\"gt\">Traceback (most recent call last):<\/span>\n<span class=\"w\">  <\/span><span class=\"c\">...<\/span>\n<span class=\"hll\"><span class=\"gr\">NameError<\/span>: <span class=\"n\">name 'math' is not defined. Did you forget to import 'math'?<\/span>\n<\/span><\/code><\/pre>\n<\/div>\n<p>When you use <code>math<\/code> without importing it first, you\u2019ll get a traditional <code>NameError<\/code>. Additionally, the parser helpfully reminds you that you need to import <code>math<\/code> before accessing it.<\/p>\n<p>The reminder about remembering to import modules only triggers for standard library modules. For these error messages, Python 3.12 doesn\u2019t track third-party packages that you\u2019ve installed.<\/p>\n<p>It\u2019s possible to import specific names from a module using a <a href=\"https:\/\/realpython.com\/python-modules-packages\/#from-module_name-import-names\"><code>from<\/code>&#8211;<code>import<\/code><\/a> statement. If you happen to switch the order of the keywords, you\u2019ll now get a friendly nudge towards the correct syntax:<\/p>\n<div class=\"highlight python repl\"><span class=\"repl-toggle\" title=\"Toggle REPL prompts and output\">&gt;&gt;&gt;<\/span><\/p>\n<pre><span><\/span><code><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"kn\">import<\/span> <span class=\"nn\">pi<\/span> <span class=\"kn\">from<\/span> <span class=\"nn\">math<\/span>\n<span class=\"go\">  ...<\/span>\n<span class=\"go\">    import pi from math<\/span>\n<span class=\"go\">    ^^^^^^^^^^^^^^^^^^^<\/span>\n<span class=\"hll\"><span class=\"go\">SyntaxError: Did you mean to use 'from ... import ...' instead?<\/span>\n<\/span><\/code><\/pre>\n<\/div>\n<p>Here, you tried to import <code>pi<\/code> from <code>math<\/code>, but Python needs you to reorder the statement and put <code>from<\/code> before <code>import<\/code>.<\/p>\n<p>To see a third new error message, check out what happens if you import <code>py<\/code> and not <code>pi<\/code> from <code>math<\/code>:<\/p>\n<div class=\"highlight python repl\"><span class=\"repl-toggle\" title=\"Toggle REPL prompts and output\">&gt;&gt;&gt;<\/span><\/p>\n<pre><span><\/span><code><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"kn\">from<\/span> <span class=\"nn\">math<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">py<\/span>\n<span class=\"gt\">Traceback (most recent call last):<\/span>\n<span class=\"w\">  <\/span><span class=\"c\">...<\/span>\n<span class=\"hll\"><span class=\"gr\">ImportError<\/span>: <span class=\"n\">cannot import name 'py' from 'math'. Did you mean: 'pi'?<\/span>\n<\/span><\/code><\/pre>\n<\/div>\n<p>There\u2019s no <code>py<\/code> name in <code>math<\/code>, so you get an <code>ImportError<\/code>. The parser suggests that you probably meant <code>pi<\/code> instead of <code>py<\/code>. Python 3.10 introduced a similar <a href=\"https:\/\/realpython.com\/python310-new-features\/#better-error-messages\">suggestion feature<\/a>, where Python looks for similar names. What\u2019s new in Python 3.12 is the ability to do this while importing.<\/p>\n<p>In addition to these three improvements related to imports, there\u2019s a final improvement concerning methods defined inside classes. Have a look at the following implementation of a <code>Circle<\/code> class:<\/p>\n<div class=\"highlight python\">\n<pre><span><\/span><code><span class=\"linenos\"> 1<\/span><span class=\"c1\"># shapes.py<\/span>\n<span class=\"linenos\"> 2<\/span>\n<span class=\"linenos\"> 3<\/span><span class=\"kn\">from<\/span> <span class=\"nn\">math<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">pi<\/span>\n<span class=\"linenos\"> 4<\/span>\n<span class=\"linenos\"> 5<\/span><span class=\"k\">class<\/span> <span class=\"nc\">Circle<\/span><span class=\"p\">:<\/span>\n<span class=\"linenos\"> 6<\/span>    <span class=\"k\">def<\/span> <span class=\"fm\">__init__<\/span><span class=\"p\">(<\/span><span class=\"bp\">self<\/span><span class=\"p\">,<\/span> <span class=\"n\">radius<\/span><span class=\"p\">):<\/span>\n<span class=\"linenos\"> 7<\/span>        <span class=\"bp\">self<\/span><span class=\"o\">.<\/span><span class=\"n\">radius<\/span> <span class=\"o\">=<\/span> <span class=\"n\">radius<\/span>\n<span class=\"linenos\"> 8<\/span>\n<span class=\"linenos\"> 9<\/span>    <span class=\"k\">def<\/span> <span class=\"nf\">area<\/span><span class=\"p\">(<\/span><span class=\"bp\">self<\/span><span class=\"p\">):<\/span>\n<span class=\"hll\"><span class=\"linenos\">10<\/span>        <span class=\"k\">return<\/span> <span class=\"n\">pi<\/span> <span class=\"o\">*<\/span> <span class=\"n\">radius<\/span><span class=\"o\">**<\/span><span class=\"mi\">2<\/span>\n<\/span><\/code><\/pre>\n<\/div>\n<p>Note that you wrongly refer to <code>radius<\/code> instead of <code>self.radius<\/code> inside <code>.area()<\/code>. This will cause an error when you call the method:<\/p>\n<div class=\"highlight python repl\"><span class=\"repl-toggle\" title=\"Toggle REPL prompts and output\">&gt;&gt;&gt;<\/span><\/p>\n<pre><span><\/span><code><span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"kn\">from<\/span> <span class=\"nn\">shapes<\/span> <span class=\"kn\">import<\/span> <span class=\"n\">Circle<\/span>\n<span class=\"gp\">&gt;&gt;&gt; <\/span><span class=\"n\">Circle<\/span><span class=\"p\">(<\/span><span class=\"mi\">5<\/span><span class=\"p\">)<\/span><span class=\"o\">.<\/span><span class=\"n\">area<\/span><span class=\"p\">()<\/span>\n<span class=\"gt\">Traceback (most recent call last):<\/span>\n<span class=\"w\">  <\/span><span class=\"c\">...<\/span>\n  File <span class=\"nb\">\"\/home\/realpython\/shapes.py\"<\/span>, line <span class=\"m\">10<\/span>, in <span class=\"n\">area<\/span>\n<span class=\"w\">    <\/span><span class=\"k\">return<\/span> <span class=\"n\">pi<\/span> <span class=\"o\">*<\/span> <span class=\"n\">radius<\/span><span class=\"o\">**<\/span><span class=\"mi\">2<\/span>\n<span class=\"w\">                    <\/span><span class=\"pm\">^^^^^^<\/span>\n<span class=\"hll\"><span class=\"gr\">NameError<\/span>: <span class=\"n\">name 'radius' is not defined. Did you mean: 'self.radius'?<\/span>\n<\/span><\/code><\/pre>\n<\/div>\n<\/div>\n<h2><a href=\"https:\/\/realpython.com\/python312-new-features\/?utm_source=realpython&#038;utm_medium=rss\">Read the full article at https:\/\/realpython.com\/python312-new-features\/ \u00bb<\/a><\/h2>\n<hr \/>\n<p><em>[ Improve Your Python With ? Python Tricks ? \u2013 Get a short &amp; sweet Python Trick delivered to your inbox every couple of days. <a href=\"https:\/\/realpython.com\/python-tricks\/?utm_source=realpython&amp;utm_medium=rss&amp;utm_campaign=footer\">&gt;&gt; Click here to learn more and see examples<\/a> ]<\/em><\/p>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this tutorial, you&#8217;ll learn about the new features in Python 3.12. You&#8217;ll explore how the new release extends the better error messages and faster code execution found in the previous version, and you&#8217;ll try out the improvements to f-strings and type variable syntax.<\/p>\n<\/div>","protected":false},"author":2055,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Python 3.12: Cool New Features for You to Try - ITTeacherITFreelance.hk","description":"In this tutorial, you'll learn about the new features in Python 3.12. You'll explore how the new release extends the better error messages and faster code execu"},"footnotes":""},"categories":[10700],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329482"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2055"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329482"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329482\/revisions"}],"predecessor-version":[{"id":329483,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329482\/revisions\/329483"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}