6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python で、超タイニー構造体

Last updated at Posted at 2014-09-04
  • タプル → 何番目が何か、よく忘れる
  • 連想配列 → メンバー名を引用符で囲むのがメンドイ
  • namedtupple → 1行とはいえ、型をいちいち作るのがメンドイ。一回限りしか使わんパターンもあるし

いちいち別途「型」を作らずに、メンバーを引用符なしの名前でアクセスできたらいいな!

struct.py
class Struct(object):
    def __init__(self,**kargs):
        for key,val in kargs.iteritems():
            setattr(self,key,val)

if __name__ == '__main__':
    t = Struct(a=10,b=20,c=30)
    t.c = "<30>"
    print t.a, t.b, t.c
<MARKEDONE:~>
✏ ipy struct.py
10 20 <30>
<MARKEDONE:~>
✏

たぶん、もっといい方法があるんだろうねぇ。亡者プログラマーだから、いろいろ忘れたよ。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?