0000 | 00 |
2004 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2005 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2006 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2007 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2008 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2009 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2010 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 10 | 11 |
2011 | 01 | 03 | 05 | 06 | 07 | 08 | 09 | 11 |
2012 | 01 | 02 | 04 |
2004 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2005 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2006 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2007 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2008 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2009 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 |
2010 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 10 | 11 |
2011 | 01 | 03 | 05 | 06 | 07 | 08 | 09 | 11 |
2012 | 01 | 02 | 04 |
2009年10月17日(土)
PythonでEnumを書いてみた。
とても車輪の再発明な気もしますが、勉強の意味の含めて自分で実装してみました。と、言っても大したものではないですけど。Enumと言っても C言語的なものではなく、JavaでのEnumを意識してます。
enum.py
# -*- coding: utf-8 -*-
class Enum(object):
@classmethod
def values(cls):
if hasattr(cls, '_values'):
return cls._values
else:
return [elm for elm in
map(lambda x: getattr(cls, x), dir(cls))
if type(elm) == cls]
@classmethod
def create(cls, names, values):
cls._values = []
for name, value in zip(names, values):
inst = cls(*value)
setattr(cls, name, inst)
cls._values.append(inst)
Enumの定義は、このクラスを継承して以下のようなクラスを書きます。
sample_enum.py
# -*- coding: utf-8 -*-
from enum import Enum
class SampleEnum(Enum):
def __init__(self, value):
self.value = value
SampleEnum.create(
(
'A', 'B', 'C', 'D', 'E',
),
(
map(lambda i: [i], range(0, 5))
)
)
使う時は、こんな感じで。
from sample_enum import SampleEnum
print SampleEnum.A
print SampleEnum.C.value
e = SampleEnum.B
if e == SampleEnum.E:
print 'false'
elif e == SampleEnum.B:
print 'true'
トラックバック - http://d.hatena.ne.jp/Horiuchi_H/20091017/1255775555
リンク元
- 280 http://blog.aodag.jp/2009/10/pythonenum.html
- 14 http://wiki.python.org/moin/JapaneseLanguage
- 5 http://blog.aodag.jp/
- 4 http://reader.livedoor.com/reader/
- 4 http://www.google.co.jp/reader/view/
- 4 http://www.google.co.jp/search?q=python+enum&hl=ja&client=firefox-a&hs=L8b&rlz=1R1GGGL_ja___JP350&prmd=df&source=lnt&tbs=lr:lang_1ja&lr=lang_ja&sa=X&ei=3c8_TJ3LIIvCcYbalYYF&ved=0CAcQpwU
- 4 http://www.google.co.jp/search?q=python+enum&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:ja:official&hl=ja&client=firefox-a
- 3 http://d.hatena.ne.jp/tonotonotono/
- 3 http://twitter.com/Horiuchi_H
- 3 http://webcache.googleusercontent.com/search?q=cache:OOZh0-K_3vAJ:blog.aodag.jp/2009/10/pythonenum.html+python+enum&cd=1&hl=ja&ct=clnk&gl=jp
PV: 485934


