Skip to content

Instantly share code, notes, and snippets.

@john1king
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john1king/9243817 to your computer and use it in GitHub Desktop.
Save john1king/9243817 to your computer and use it in GitHub Desktop.

转换对象为字典和列表

只要对象定义了 keys 方法,就使用 keys__getitem__ 来生成 dict 没有定义 keys 方法的对象,尝试先使用 __iter__ 转换为 list 后再转换为 dict

class Properties(object):

    def __init__(self, data):
        self._data = data
    
    def __iter__(self):
        return iter(list(self._data.values()))
    
    def __getitem__(self, key):
        return self._data[key]
    
    def keys(self):
        return list(self._data)


prop = Properties({'foo': 'bar'})
print dict(prop) # => {'foo': 'bar'}
print list(prop) # => ['bar']

对象转换为 list 优先迭代 __iter__ 方法,没有定义 __iter__ 方法的, 尝试用__getitem__方法索引对象,直到捕捉到 IndexError。 两种方式都会调用 __len__ 方法,但是不会使用 __len__ 的返回值来做索引

class Foo(object):

    def __getitem__(self, key):
        if key < 3:
            return key
        raise IndexError

print list(Foo()) # => [0, 1, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment