Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?
@kaizen_nagoya

pythonのcodingを調べ試して

More than 3 years have passed since last update.

pythonの先頭の


# coding: utf-8

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

例題は

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

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を```の後に記載

2
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  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
kaizen_nagoya
I'm a network designer.I work on TOPPERS SmallestSetProfile Kernel,MISRA-C, STARC RTL Design StyleGuide (Verilog-HDL),HAZOP,ISO/IEC15504(AutomotiveSPICE),ISO26262. I was an editor on ISO/IEC 15504.

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
2
Help us understand the problem. What is going on with this article?