Accessing dict keys like an attribute?

前端 未结 27 2151
南笙
南笙 2020-11-22 04:22

I find it more convenient to access dict keys as obj.foo instead of obj[\'foo\'], so I wrote this snippet:

class AttributeDict(dict         


        
27条回答
  •  鱼传尺愫
    2020-11-22 05:08

    This is what I use

    args = {
            'batch_size': 32,
            'workers': 4,
            'train_dir': 'train',
            'val_dir': 'val',
            'lr': 1e-3,
            'momentum': 0.9,
            'weight_decay': 1e-4
        }
    args = namedtuple('Args', ' '.join(list(args.keys())))(**args)
    
    print (args.lr)
    

提交回复
热议问题