Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the official name of this construct?

Tags:

python

In python:

>>> a = b or {}
like image 484
Stefano Borini Avatar asked Feb 24 '10 08:02

Stefano Borini


People also ask

What is a construct in CDK?

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.

What is construct ID?

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.

What is L2 construct?

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.

What is AWS construct library?

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.


2 Answers

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 {}
like image 78
Mark Byers Avatar answered Oct 04 '22 06:10

Mark Byers


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 nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or 'foo' yields the desired value. Because not 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' yields False, not ''.)

like image 24
Gumbo Avatar answered Oct 04 '22 08:10

Gumbo