In python:
>>> a = b or {}
Constructs are the basic building blocks of AWS CDK apps. A construct represents a "cloud component" and encapsulates everything AWS CloudFormation needs to create the component.
Construct IDs The most common identifier, id , is the identifier passed as the second argument when instantiating a construct object. This identifier, like all identifiers, need only be unique within the scope in which it is created, which is the first argument when instantiating a construct object.
L2 construct The construct creates a Lambda function with automatic transpiling and bundling of TypeScript or Javascript code. This results in smaller Lambda packages that contain only the code and dependencies needed to run the function, and it uses esbuild under the hood.
The AWS Solutions Constructs library is an open-source extension of the AWS Cloud Development Kit (AWS CDK) that provides multi-service, well-architected patterns for quickly defining solutions in code to create predictable and repeatable infrastructure.
I don't think it has an official name, it's just a clever/lazy way to be concise. It's roughly equivalent to:
a = b if b else {}
or:
if b:
a = b
else:
a = {}
I wrote this as a comment but I think it's worth mentioning here:
You have to be very careful when using this trick. If your intention is to set a default value when something is None, it would be better to write that is None
test explicitly than use this trick. The reason is that None is not the only thing that evaluates to false in a boolean context. 0, [], {}, ... also evaluate to false. So this trick might not give you the expected results if you didn't think about these special cases.
Here's one way to do it that is safer:
a = b if b is not None else {}
The expression b or {}
is just a simple boolean OR expression. The special about this is just that this expression does not need return a boolean value per definition like in other languages:
The expression
x or y
first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.(Note that neither
and
noror
restrict the value and type they return toFalse
andTrue
, but rather return the last evaluated argument. This is sometimes useful, e.g., ifs
is a string that should be replaced by a default value if it is empty, the expressions or 'foo'
yields the desired value. Becausenot
has to invent a value anyway, it does not bother to return a value of the same type as its argument, so e.g.,not 'foo'
yieldsFalse
, not''
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With