Help us understand the problem. What is going on with this article?

pythonのcodingを調べ試して

More than 1 year has passed since last update.

pythonの先頭の

# coding: utf-8

単なるおまじないかどうか試してみた。

例題は

『ゼロから作るDeep Learning』斉藤康穀 著 オライリージャパン, 2016

https://www.oreilly.co.jp/books/9784873117584/

68747470733a2f2f71696974612d696d6167652d73746f72652e73332e616d617a6f6e6177732e636f6d2f302f35313432332f38303639613938632d616564332d363066342d346430392d6262376430626331623161392e6a706567.jpeg
見本算譜(sample program)はWebに上がっている。
https://github.com/oreilly-japan/deep-learning-from-scratch

deep-learning-from-scratch/ch01/

man.py
# coding: utf-8 
 class Man: 
     """サンプルクラス""" 

     def __init__(self, name): 
         self.name = name 
         print("Initilized!") 

     def hello(self): 
         print("Hello " + self.name + "!") 
       def goodbye(self): 
         print("Good-bye " + self.name + "!") 

 m = Man("David") 
 m.hello() 
 m.goodbye() 

文字コードはutf-8。

異なる文字コードを基本としているシステムではコード変換が必要かも。

$ nkf -s man.py > mans.py
$ python mans.py
  File "mans.py", line 3
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0x83 in position 0: invalid start byte

それではと、coding:のあとを書き換えてみた。

mans.py
# coding: shift-jis 
 class Man: 
     """サンプルクラス""" 

     def __init__(self, name): 
         self.name = name 
         print("Initilized!") 

     def hello(self): 
         print("Hello " + self.name + "!") 
       def goodbye(self): 
         print("Good-bye " + self.name + "!") 

 m = Man("David") 
 m.hello() 
 m.goodbye() 
$ python mans.py
Initilized!
Hello David!
Good-bye David!

おまじないではなく、指定の文字コードを読んで、実際の文字コードと同じかどうか確認しているようだ。

参考文献・URL

Pythonのcoding: ナントカのことがよく分からないから調べてみた
https://qiita.com/ronin_gw/items/2c82b727461b18991eff

Pythonで"coding: utf-8" 書くの面倒くさいのでShellscriptでなんとかする
https://qiita.com/esehara@github/items/8662a0db94a7999a52c0

Python初歩からの学習メモ1
https://qiita.com/sesame/items/47d2694616eadb35d4bc

pythonのヘッダ
https://qiita.com/koteran/items/3dba8469641482c2c4f6
指摘が具体的なら、そのままコピペできる。抽象的な指摘は先に進まない例。

文書履歴

20180316 0.10 初版
20180316 0.11 shell, pythonを```の後に記載

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away