pep8 compliant deep dictionary access

后端 未结 2 1435
野趣味
野趣味 2020-12-11 01:09

What is the pep8 compliant way to do deep dictionary access?

dct = {
    \'long_key_name_one\': {
        \'long_key_name_two\': {
            \'long_key_nam         


        
相关标签:
2条回答
  • 2020-12-11 01:39

    If you use it inside a function (and you could use print() as a function since 2.7 afaik)

    You could just use implicit concatenation within a parentheses

    print(dct['long_key_name_one']
             ['long_key_name_two']
             ['long_key_name_three']
             ['long_key_name_four']
             ['long_key_name_five'])
    
    0 讨论(0)
  • 2020-12-11 02:01

    Perhaps not the best way, but it works:

    a = dct['long_key_name_one']['long_key_name_two']
    b = a['long_key_name_three']['long_key_name_four']['long_key_name_five']
    

    But this also works, which is the suggested method:

    print (dct['long_key_name_one']['long_key_name_two']
           ['long_key_name_three']['long_key_name_four']
           ['long_key_name_five'])
    
    0 讨论(0)
提交回复
热议问题