Namedtuple喜欢上课

时间:2011-02-27 02:29:31

标签: python

当我需要一个快速的单用类时,我发现自己经常在我的python代码中编写这个类。

class Struct(object):
   def __init__( self, **kwargs ):
      for k in kwargs:
         setattr(self,k,kwargs[k])

基本的想法是这样我可以做这样快速的事情:

foo = Struct( bar='one', baz=1 )
print foo.bar
foo.baz += 1
foo.novo = 42 # I don't do this as often.

当然这不能很好地扩展,添加方法只是疯了,但即便如此,我仍然有足够的数据丢弃类,我一直在使用它。

这就是我认为的namedtuple。但是,这个名字的语法很庞大而且很笨拙。

标准库中是否有一些我还没有找到的东西能做到这一点还是更好?

这是一种糟糕的坏风格吗?还是有一些隐藏的缺陷?

更新

两个具体的例子,说明为什么我不只是使用字典。这两个例子都可以用dict完成,但显然不是惯用的。

#I know an order preserving dict would be better but they don't exist in 2.6.
closure = Struct(count=0)
def mk_Foo( name, path ):
   closure.count += 1
   return (name, Foo( name, path, closure.count ))

d = dict([
   mk_Foo( 'a', 'abc' ),
   mk_Foo( 'b', 'def' ),
   # 20 or so more
   ] )


@contextmanager
def deleter( path ):
   control = Struct(delete=True,path=path)
   try:      
      yield control
   finally:
      if control.delete:
         shutil.rmtree(path)

with deleter( tempfile.mkdtemp() ) as tmp:
   # do stuff with tmp.path

   # most contexts don't modify the delete member
   # but occasionally it's needed
   if keep_tmp_dir:
      tmp.delete = False

5 个答案:

答案 0 :(得分:9)

有一个python配方(它只是更新实例的dict而不是调用setattr) Recipe 52308

class Bunch(object):
    def __init__(self, **kwds):
        self.__dict__.update(kwds)

答案 1 :(得分:7)

从Python 3.3及之后,您可以使用types.SimpleNamespace

>>> import types
>>> foo = types.SimpleNamespace(bar='one', baz=1)
>>> print(foo.bar)
one
>>> foo.baz += 1
>>> foo.novo = 42

内置类型大致相当于以下代码:

class SimpleNamespace:

    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

<强>更新

从Python 3.7开始,您可以使用 dataclass 模块:

from dataclasses import dataclass, field

@dataclass
class Struct:
    bar: str = field(default='one')
    baz: int = field(default=1)

您可以按如下方式使用:

foo = Struct( bar='one', baz=1 )
print(foo.bar)
foo.baz += 1
foo.novo = 42

默认情况下,它包含相等测试和漂亮的repr:

>>> foo == Struct(bar='one', baz=2)
True
>>> foo
Struct(bar='one', baz=2)

答案 2 :(得分:3)

class t(dict):

   def __init__(self, **kwargs):
      for key, value in kwargs.items():
         dict.__setitem__(self, key, value)
   def __getattr__(self, key):
      return dict.__getitem__(self, key)
   def __setattr__(self, key, value):
      raise StandardError("Cannot set attributes of tuple")      
   def __setitem__(self, key, value):
      raise StandardError("Cannot set attributes of tuple")      
   def __delitem__(self, key):
      raise StandardError("Cannot delete attributes of tuple")

point = t(x=10, y=500, z=-50)
print point.x        # 10
print point.y        # 500
print point['z']     # -50
print point          # {'z': -50, 'y': 500, 'x': 10}
point.x = 100        # StandardError: cannot set attributes of tuple
point.y += 5         # StandardError: cannot set attributes of tuple
point.z = -1         # StandardError: cannot set attributes of tuple

def hypo(x, y, z):
   return (x**2 + y**2 + z**2)**0.5

print hypo(point)    # TypeError: unsupported operand type(s)
print hypo(**point)  # 502.593274925   

for k in point.items():
   print k           # ('y', 500)
                     # ('x', 10)
                     # ('z', -50)

for k in point.keys():
   print k           # x
                     # y
                     # z

for k in point.values():
   print k           # 500
                     # 10
                     # -50

print len(point)     # 3

print dict(point)    # {'y': 500, 'x': 10, 'z': -50}

这是我解决这个问题的方法。美丽的语法,不可变(至少没有诉诸一些令人讨厌的对象。 setattr ()体操),轻巧,漂亮可打印。虽然没有什么可以做的,你不能用dict,

point = t(x=10, y=20, z=30)
d = point.x ** 2 + point.y ** 2 + point.z ** 2

有很好的对称性
point = (10, 20, 30)
d = point[0] ** 2 + point[1] ** 2 + point[2] ** 2

总体而言比

更清洁
point = {'x': 10, 'y': 20, 'z': 30}
d = point['x'] ** 2 + point['y'] ** 2 + point['z'] ** 2

答案 3 :(得分:1)

你所拥有的是一个完全合理的原型,但你不对它进行扩展。

如果你喜欢使用它们,但希望以后有更好的代码路径,这就是我的建议:

  • 每次执行此操作时,子类结构:

    class Control(Structure): pass

  • 稍后,当你需要一个“真正的”类时,用提供相同构造函数和属性接口的strongbox.Strongboxexample usage)替换超类,但是限制你可以使用哪个插槽填写。

这样的训练只会花费你额外的一行,如果你以后想要更多的力量,也不会破坏你的代码。

答案 4 :(得分:1)

您可能希望Records查看George Sakkis。它对我来说效果很好,是一个“可变的命名元组。”