Skip to content

Instantly share code, notes, and snippets.

@liuyix
Last active December 30, 2015 16:09
Show Gist options
  • Save liuyix/7853143 to your computer and use it in GitHub Desktop.
Save liuyix/7853143 to your computer and use it in GitHub Desktop.
python best practise!

Python Tips

建立class的最佳实践

credit:http://stackoverflow.com/a/62680

class Foo(object):
    #...

这样的建立方式,可以是Foo类自带__dict__,其中包含了所有类成员的字典,因此可以使用下面的语句: pack_info = dict((key, p.__dict__[key]) for key in Package.json_members) 实现字典的部分拷贝

dict判断是否具有某些key

>> mydict = {'foo': 'foo', 'bar': 'bar', 'hi':'hi'}
>> set(['foo', 'bar']) <= set(mydict)
True

排序——lambda匿名函数

result = sorted(memory_dict, key=lambda elem: elem[0]+elem[1], reverse=True)

link: http://docs.python.org/2/tutorial/controlflow.html#lambda-expressions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment