SSブログ

ああ、なぜこんなことに気がつかなかったんだろう [Python]

Pythonでタートルグラフィックスについて
https://docs.python.jp/3/library/turtle.html
にサンプルがある。このコードが実行できなかった理由がわかった。わかってみれば、そりゃそうだ。ここのコード例を保存する時に、そのファイルをturtle.pyとしてしまっていた。つまりfrom turtle import *とごっちゃになってしまうと、ダメなのだ。ここに解説がされていた。

https://stackoverflow.com/questions/11346013/turtle-module-has-no-attribute-color

"You named your file "turtle.py" so when you import turtle, you are importing your own file instead of the stdlib module. Change the name of your program, and delete all the .pyc files in that directory."

このことにふれている本があるか探してみたら、"Head First Learn to Code"の309ページの"watch it!"にも説明されていた。

Head First Learn to Code

Head First Learn to Code

  • 作者: Eric Freeman
  • 出版社/メーカー: Oreilly & Associates Inc
  • 発売日: 2018/01/12
  • メディア: ペーパーバック



nice!(0)  コメント(0) 
共通テーマ:日記・雑感