<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
  <author_name>zehnpaard</author_name>
  <author_url>https://blog.hatena.ne.jp/zehnpaard/</author_url>
  <blog_title>Arantium Maestum</blog_title>
  <blog_url>https://zehnpaard.hatenablog.com/</blog_url>
  <categories>
    <anon>Python</anon>
  </categories>
  <description>ツイッターでPythonの文法の話題が盛ん（三角関数と同じで周期的に話題になる）で、個人的にはあまり同意できない意見も多いのだがなかなか面白いトピックもある。 例えば以下のようなコード： fs = [] for i in range(5): fs.append(lambda: i) for f in fs: print(f()) これが多くの人の直観に反して4を5回出力するという挙動になる。 このような挙動になる理由は複合的で： 変数iのスコープがfor内のブロックで切れていない lambda: iで定義された関数がクロージャとしてlexical scopeで変更され続ける変数iを捕捉している…</description>
  <height>190</height>
  <html>&lt;iframe src=&quot;https://hatenablog-parts.com/embed?url=https%3A%2F%2Fzehnpaard.hatenablog.com%2Fentry%2F2022%2F05%2F27%2F080010&quot; title=&quot;Pythonで変数スコープがブロックベースだと大変そう - Arantium Maestum&quot; class=&quot;embed-card embed-blogcard&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;&quot;&gt;&lt;/iframe&gt;</html>
  <image_url></image_url>
  <provider_name>Hatena Blog</provider_name>
  <provider_url>https://hatena.blog</provider_url>
  <published>2022-05-27 08:00:10</published>
  <title>Pythonで変数スコープがブロックベースだと大変そう</title>
  <type>rich</type>
  <url>https://zehnpaard.hatenablog.com/entry/2022/05/27/080010</url>
  <version>1.0</version>
  <width>100%</width>
</oembed>
