みんなが言及しないPython 3.0のステキ機能
by
ats
—
posted at
2008-12-05 10:08
last modified
2008-12-05 16:19
Python 3.0 (r30:67503, Dec 5 2008, 09:49:50)普通に見えます? じゃあ2.5とかでこれと同じことやってみれ。
[GCC 4.0.1 (Apple Inc. build 5484)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> l=['にんとも', 'かんとも', 'ニンニン']
>>> l
['にんとも', 'かんとも', 'ニンニン']
Python 2.5.2 (r252:60911, Feb 22 2008, 07:57:53)c.f.
[GCC 4.0.1 (Apple Computer, Inc. build 5363)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> l=[u'にんとも', u'かんとも', u'ニンニン']
>>> l
[u'\u306b\u3093\u3068\u3082', u'\u304b\u3093\u3068\u3082', u'\u30cb\u30f3\u30cb\u30f3']
http://www.python.org/dev/peps/pep-3138/
- Category(s)
- python
- The URL to Trackback this entry is:
- http://coreblog.org/ats/finest-new-neature-of-python-30/tbping






>>> 漢字=123
>>> print(漢字)
123
わはは,こりゃ傑作だ:-)。
>>> class 動物:
>>> 鳴き声 = '・・・'
>>> def 鳴く(自分):
>>> print('%s:%s' % (自分.__class__.__name__, 自分.鳴き声))
>>>
>>> class 猫(動物):
>>> 鳴き声 = 'にゃー'
>>>
>>> class 熊(動物):
>>> 鳴き声 = 'クマー'
>>>
>>> 君のペット = 猫()
>>> 君のペット.鳴く()
猫:にゃー
>>> 僕のペット = 熊()
>>> 僕のペット.鳴く()
熊:クマー
>>> 五
2
これはOKなのはいいのですが、(結構感動ですが)
>>> 5 = 2
File "
5 = 2
^
SyntaxError: invalid character in identifier
と、怒られます。
>>> 5 = 2
File "
SyntaxError: can't assign to literal
これとも、エラーメッセージが少し違う感じなんですよねー。
いろいろやってみると、変数名として使えない1バイト文字との対応を見ているように感じるのですが、はっきりとは分かりません。
http://unicode.org/Public/UNIDATA/UCD.html#General_Category_Values
Python 3.0になってもPEP 8は生きているので,クラス名に「動物」はありえないです(笑)。