Files
flypythoncom.github.io/article/python-new-02/index.html
2020-01-19 12:07:38 +08:00

166 lines
11 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<head><meta name="generator" content="Hexo 3.9.0">
<!-- Title -->
<meta charset="utf-8">
<meta name="applicable-device" content="pc,mobile">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=3.0, viewport-fit=cover">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="author" content="flypython">
<meta name="designer" content="flypython">
<meta name="keywords" content="Python 3.8新特性——仅限位置形参,FlyPython - 专业的Python学习社区,flypython, 飞蟒飞蟒PythonPython入门Python自动化Python日报">
<meta property="og:title" content="Python 3.8新特性——仅限位置形参 | FlyPython - 专业的Python学习社区">
<meta property="og:site_name" content="http://www.flypython.com">
<meta property="og:type" content="article">
<meta property="og:url" content="http://www.flypython.com/article/python-new-02/">
<meta property="og:image" content="http://www.flypython.com/images/new-02.png">
<meta property="og:description" content="Python 3.8新特性——仅限位置形参--介绍Python语言新的特性">
<meta name="description" content="Python 3.8新特性——仅限位置形参--介绍Python语言新的特性">
<meta name="rating" content="general">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="robots" content="index, follow">
<link rel="icon" href="/images/favicon.ico">
<title>Python 3.8新特性——仅限位置形参 | FlyPython - 专业的Python学习社区</title>
<link rel="stylesheet" href="/css/f25.css">
<link rel="stylesheet" href="/css/highlight.css">
<link rel="stylesheet" href="/css/gitalk.css">
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-147288599-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-147288599-1');
</script>
</head>
</head>
<body>
<header class="wrapper header-wrapper">
<div class="container header-nav-wrapper">
<div class="logo"><a href="/" title="FlyPython - 专业的Python学习社区"><h1 class="title">FlyPython</h1></a></div>
<nav class="nav-wrapper">
<a href="https://flypython.com/python" title="飞蟒微课堂">飞蟒微课堂</a>
<a href="https://flypython.com/flypython_daily" title="Python日报">Python日报</a>
<a href="https://flypython.com/PyCon/" title="PyCon">PyCon</a>
<a href="https://github.com/flypythoncom" title="Github">Github</a>
<a href="/article/about" title="关于">关于</a>
</nav>
<span class="btn-menu" id="J_header_menu">
<div class="inner">
<span class="line line-01"></span>
<span class="line line-02"></span>
<span class="line line-03"></span>
</div>
</span>
<div class="wrapper mb-nav-wrapper" id="J_header_menu_list">
<nav class="wrapper mb-nav-container">
<a href="https://flypython.com/python" title="飞蟒微课堂">飞蟒微课堂</a>
<a href="https://flypython.com/flypython_daily" title="Python日报">Python日报</a>
<a href="https://flypython.com/PyCon/" title="PyCon">PyCon</a>
<a href="https://github.com/flypythoncom" title="Github">Github</a>
<a href="/article/about" title="关于">关于</a>
</nav>
</div>
</div>
</header>
<section class="body-wrapper">
<section class="wrapper post-banner">
<div class="container post-banner-container">
<h2 class="wrapper title">Python 3.8新特性——仅限位置形参</h2>
<div class="wrapper tips">
<span>Author</span><span>flypython</span> | <span>Date: </span><span>2019-04-02</span> | <span>Category</span><span><a href="/fly/Python新特性/" title="Python新特性">Python新特性</a></span>
</div>
</div>
</section>
<section class="wrapper main-wrapper">
<article class="sub-container post-content">
<h2 id="仅限位置形参"><a href="#仅限位置形参" class="headerlink" title="仅限位置形参"></a>仅限位置形参</h2><p>Positional-only parameters官方翻译为仅限位置形参也可以理解为只接受位置参数。意思就是它只是一个位置参数不接受关键字传参。</p>
<p>语法: </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">def funx(a,b,/): # / 指明前面的a,b参数是仅限位置形参</span><br><span class="line"> pass</span><br></pre></td></tr></table></figure>
<p>函数形参语法<code>/</code> 用来指明某些函数形参必须使用仅限位置而非关键字参数</p>
<p>其实Python内置的很多C函数接口都是这种形式比如</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; import builtins</span><br><span class="line">&gt;&gt;&gt; help(__builtins__.divmod)</span><br><span class="line">Help on built-in function divmod in module builtins:</span><br><span class="line"></span><br><span class="line">divmod(x, y, /)</span><br><span class="line"> Return the tuple (x//y, x%y). Invariant: div*y + mod == x.</span><br></pre></td></tr></table></figure>
<p>很多函数后面都有 <code>/</code>来表明,左边的这些参数只接受位置参数。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; divmod(1,2)</span><br><span class="line">(0, 1)</span><br><span class="line">&gt;&gt;&gt; divmod(x=1,y=2) </span><br><span class="line">Traceback (most recent call last):</span><br><span class="line"> File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;</span><br><span class="line">TypeError: divmod() takes no keyword arguments</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure>
<p>指定关键字的参数会报语法错误,它的用途就是强制使用者用位置参数来传参。</p>
<h4 id="官方例子"><a href="#官方例子" class="headerlink" title="官方例子"></a>官方例子</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; def f(a,b,/,**kwargs):</span><br><span class="line">... print(a,b,kwargs)</span><br><span class="line">...</span><br><span class="line">&gt;&gt;&gt; f(10,20,a=1,b=2,c=3)</span><br><span class="line">10 20 &#123;&apos;a&apos;: 1, &apos;b&apos;: 2, &apos;c&apos;: 3&#125;</span><br></pre></td></tr></table></figure>
<p>由于在 <code>/</code> 左侧的形参不会被公开为可用关键字</p>
<p>这里的a,b 为仅限位置参数最后a,b会被赋值了两次。<br>位置参数赋值一次关键字参数赋值一次关键字参数以kwargs字典的形式存在需要通过 <code>kwargs[&#39;a&#39;],kwargs[&#39;b&#39;]</code>访问。</p>
<p>现在我们来看一下,添加了仅限位置形参之后的函数参数形式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">def name(positional_only_parameters, /, positional_or_keyword_parameters,</span><br><span class="line"> *, keyword_only_parameters):</span><br></pre></td></tr></table></figure>
<p>包括了仅限位置形参, <code>/</code>, 位置形参或者关键字参数 ,<code>*</code>,仅限关键字参数。</p>
<p><img src="https://tva1.sinaimg.cn/large/006tNbRwly1gai73g9r3ej30so09mq3k.jpg" alt></p>
<p>最后,我们可以定义以下形式的函数</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">def name(p1, p2, /, p_or_kw, *, kw):</span><br><span class="line">def name(p1, p2=None, /, p_or_kw=None, *, kw):</span><br><span class="line">def name(p1, p2=None, /, *, kw):</span><br><span class="line">def name(p1, p2=None, /):</span><br><span class="line">def name(p1, p2, /, p_or_kw):</span><br><span class="line">def name(p1, p2, /):</span><br></pre></td></tr></table></figure>
<h4 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h4><ul>
<li><a href="https://docs.python.org/zh-cn/3.8/whatsnew/3.8.html" target="_blank" rel="noopener">https://docs.python.org/zh-cn/3.8/whatsnew/3.8.html</a></li>
<li><a href="https://docs.python.org/zh-cn/3/howto/clinic.html" target="_blank" rel="noopener">https://docs.python.org/zh-cn/3/howto/clinic.html</a></li>
<li><a href="https://www.python.org/dev/peps/pep-0570" target="_blank" rel="noopener">https://www.python.org/dev/peps/pep-0570</a></li>
</ul>
</article>
<div class="sub-container gitalk-wrapper" id="gitalk-container"></div>
</section>
<div class="tips-top-wrapper">
<span class="tip-top-container" onclick="scrollToWindowTop()">
<span class="l-bar"></span>
<span class="r-bar"></span>
</span>
</div>
<footer class="wrapper footer-wrapper">
<div class="container"><span class="copyright">&copy; 2020 FlyPython . All Rights Reserved.</span></div>
</footer>
</section>
<script src="/js/f25.js"></script>
<script src="/js/gitalk.min.js"></script>
<script>
var gitalkAdmin = 'xxg1413'.split(',');
var gitalk = new Gitalk({
clientID: 'd0e566bfc45c0b852c6c',
clientSecret: '6b69b3a841c85a6223e5a904c47f5e2d84322980',
repo: 'gitalk',
owner: 'flypythoncom',
admin: gitalkAdmin,
id: location.pathname.length > 50 ? location.pathname.substr(0,50) : location.pathname, // Ensure uniqueness and length less than 50
distractionFreeMode: false // Facebook-like distraction free mode
});
gitalk.render('gitalk-container');
</script>
</body>
</html>