未定义全局名称python 2.7在类中定义并在函数中使用

时间:2019-06-27 18:01:16

标签: python-2.7 function dictionary global nameerror

以下代码在Python 3上有效,但在python 2.7上无效。在Python 2.7中,我得到NameError:未定义全局名称'case_types'。 但是,如果我取消注释#global case_types,则可以在Python 2.7上运行。有没有办法在不声明全局代码的情况下使此代码运行?谢谢

class list_variable_test():
#global case_types 


case_types = {
'BOARD OF REVISIONS': 'BR',
'CIVIL': 'CV',
'DOMESTIC RELATIONS': 'DR',
'GARNISHMENT': 'GR',
'JUDGMENT LIEN': 'JL',
'MISCELLANEOUS-CLAIMS': 'MS',
'SPECIAL DOCKET': 'SD',}

def search():
    #print(case_types.keys())
    #case_types.keys()
    pass


    def get_case_type_val(case_type_key=None,party_role_key=None):
        #print(case_types)
        returnVal = case_types.get(case_type_key,"")
        return returnVal
        pass

    casetypeVal = get_case_type_val(case_type_key='DOMESTIC RELATIONS',party_role_key=None)
    print(casetypeVal)
search()

0 个答案:

没有答案