这可能是一个愚蠢的问题,但是我想知道是否有一种方法可以打印或使用在if语句中被评估的值,而不必在评估之前先将其存储在变量中?
类似于以下语法:
if value_of_really_long_expression == True:
print(value_of_really_long_expression)
当然可以通过以下方式实现:
x = value_of_really_long_expression
if x == True:
print(x)
纯粹出于兴趣/好奇,我想知道这是否有可能
答案 0 :(得分:3)
这是不可能的;没有可以使用的隐式引用。不过,Python 3.8引入了一个赋值表达式,至少可以简化赋值:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "org.techtown.samplelayoutinflater1"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-
optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
if (x := really_long_expression):
print(x)
的范围与您编写的范围相同
x
也就是说,x = really_long_expression
if x:
print(x)
不在x
语句的主体中。