首页 > 解决方案 > 如果太长,是否可以剪切长变量名?

问题描述

我有一个很长的命名变量,是否可以将其剪切,例如一半?

aquiredCostsOfIncomeInSamePlaceOfLiveAndWorkPersonUnderTwentySixNotAStudent = 250

我已经尝试过,但它不起作用:

  1. 反斜杠:

    aquiredCostsOfIncomeInSamePlaceOf\
    LiveAndWorkPersonUnderTwentySixNotAStudent = 250
    
  2. 反斜杠和括号:

    (aquiredCostsOfIncomeInSamePlaceOf\
    LiveAndWorkPersonUnderTwentySixNotAStudent = 250)
    
  3. 加:

    aquiredCostsOfIncomeInSamePlaceOf+
    LiveAndWorkPersonUnderTwentySixNotAStudent = 250
    
  4. 括号和引号:

    ('aquiredCostsOfIncomeInSamePlaceOfLive
    AndWorkPersonUnderTwentySixNotAStudent') = 250
    
  5. 括号和更多引号:

    ('aquiredCostsOfIncomeInSamePlaceOf'
    'LiveAndWorkPersonUnderTwentySixNotAStudent') = 250
    

我发现唯一有效的是:

aquiredCostsOfIncomeInSamePlaceOfLiveAndWorkPersonUnderTwentySixNotAStudent\ 
= 250

但我总是可以在“=”括号之后剪切。

你知道有什么解决办法吗?

标签: pythonpython-3.x

解决方案


有一种方法可以做到这一点,但不是传统方式:使用字典。

my_vars = {
'aquiredCostsOfIncome' \
'InSamePlaceOfLiveAnd'\
'WorkPersonUnderTwentySix'\
'NotAStudent': 250
}
print(my_vars['aquiredCostsOfIncomeInSamePlaceOfLiveAndWorkPersonUnderTwentySixNotAStudent'])

(注意:不要这样做。只是取一个较短的名字)


推荐阅读