Difference between revisions of "Python assignment operators"

From KoanSoftware Wiki
Jump to: navigation, search
(Created page with "== Python assignment operators == - *Variable Setting (=)* Set a variable by assigning it a value using the *=* sign. Values must be surrounded by double quotes. * VAR = "...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== Python assignment operators ==
 
== Python assignment operators ==
  
-
 
  
*Variable Setting (=)*
+
* Variable Setting (=)
  
 
Set a variable by assigning it a value using the *=* sign. Values must
 
Set a variable by assigning it a value using the *=* sign. Values must
 
be surrounded by double quotes.
 
be surrounded by double quotes.
  
*
+
VAR = "value"
VAR = "value"
 
  *
 
  
 
In this example, *VAR* is set to *value*.
 
In this example, *VAR* is set to *value*.
-
 
  
*Early Default Value Assignment (?=)*
+
 
 +
----
 +
 
 +
 
 +
* Early Default Value Assignment (?=)
  
 
A variable can be early-assigned a default value using the *?=* assignment
 
A variable can be early-assigned a default value using the *?=* assignment
 
operator:
 
operator:
  
*
+
A ?= "1"
A ?= "1"
+
B ?= "2"
B ?= "2"
+
B ?= "3"
B ?= "3"
 
  *
 
  
 
In this example *A* will contain *1* if it was not previously set. *B* will
 
In this example *A* will contain *1* if it was not previously set. *B* will
Line 30: Line 28:
 
that if there are multiple *?=* assignments to a single variable, the
 
that if there are multiple *?=* assignments to a single variable, the
 
first one will be used.
 
first one will be used.
-
 
  
*Late Default Value Assignment (??=)*
+
 
 +
----
 +
 
 +
 
 +
* Late Default Value Assignment (??=)
  
 
A variable can be late-assigned a default value using the *??=* assignment
 
A variable can be late-assigned a default value using the *??=* assignment
 
operator:
 
operator:
  
*
+
A ??= "1"
A ??= "1"
+
B ??= "2"
B ??= "2"
+
B ??= "3"
B ??= "3"
 
  *
 
  
 
In this example *A* will contain *1* if it was not previously set. *B* will
 
In this example *A* will contain *1* if it was not previously set. *B* will
Line 48: Line 47:
 
parsing process. If there are multiple *??=* assignments to a single
 
parsing process. If there are multiple *??=* assignments to a single
 
variable, the last one will be used.
 
variable, the last one will be used.
-
 
  
*Variable Expansion*
+
A ?= following a ??= will set the variable. Only a = will be
 +
override the setting of the ?=. Any following ?= and ??= will not change
 +
the setting.
 +
 
 +
 
 +
----
 +
 
 +
 
 +
* Variable Expansion
  
 
Bitbake supports referencing a variables content similar to shell
 
Bitbake supports referencing a variables content similar to shell
 
scripting.
 
scripting.
  
*
+
A = "jumps over"
A = "jumps over"
+
B = "The quick brown fox ${A} the lazy dog."
B = "The quick brown fox ${A} the lazy dog."
+
echo $B
echo $B
+
 
  *
 
  
 
This results in the the text *The quick brown fox jumps over the lazy
 
This results in the the text *The quick brown fox jumps over the lazy
 
dog.* printed to the console.
 
dog.* printed to the console.
-
 
  
*Immediate Variable Expansion (:=)*
+
 
 +
----
 +
 
 +
 
 +
* Immediate Variable Expansion (:=)
  
 
Typically, Bitbake expands a variable when it is used. The *:=* assignment
 
Typically, Bitbake expands a variable when it is used. The *:=* assignment
 
operator however expands the variable immediately when it is assigned.
 
operator however expands the variable immediately when it is assigned.
  
*
+
A = "11"
A = "11"
+
B = "B:${A}"
B = "B:${A}"
+
A = "22"
A = "22"
+
C := "C:${A}"
C := "C:${A}"
+
echo $C $B
echo $C $B
 
  *
 
  
 
This results in *C:22 B:22* printed to the console since the content of *
 
This results in *C:22 B:22* printed to the console since the content of *
 
A* is expanded immediately on assignment to variable *C*. Variable *B* did
 
A* is expanded immediately on assignment to variable *C*. Variable *B* did
 
not get expanded until it was actually used in the *echo*statement.
 
not get expanded until it was actually used in the *echo*statement.
-
 
  
*Appending (+=) and Prepending (=+)*
+
 
 +
----
 +
 
 +
 
 +
* Appending (+=) and Prepending (=+)
  
 
The *+=* and *=+* append and prepend variables respectively while adding
 
The *+=* and *=+* append and prepend variables respectively while adding
 
a single space between the values.
 
a single space between the values.
  
*
+
A = "12"
A = "12"
+
A += "34"
A += "34"
+
B = "89"
B = "89"
+
B =+ "67"
B =+ "67"
 
  *
 
  
 
This results in variable *A* containing *12 34* and variable *B*
 
This results in variable *A* containing *12 34* and variable *B*
 
containing *67 89*.
 
containing *67 89*.
-
 
  
*Appending (.=) and Prepending (=.) without Spaces*
+
 
 +
----
 +
 
 +
 
 +
* Appending (.=) and Prepending (=.) without Spaces
  
 
The *.=* and *=.* work like the above appending and prepending operators
 
The *.=* and *=.* work like the above appending and prepending operators
 
however without placing an additional space between the values.
 
however without placing an additional space between the values.
  
*
+
A = "12"
A = "12"
+
A .= "34"
A .= "34"
+
B = "89"
B = "89"
+
B =. "67"
B =. "67"
 
  *
 
  
 
This results in variable *A* containing *1234* and variable *B*
 
This results in variable *A* containing *1234* and variable *B*
 
containing *6789*.
 
containing *6789*.

Latest revision as of 13:52, 15 May 2019

Python assignment operators

  • Variable Setting (=)

Set a variable by assigning it a value using the *=* sign. Values must be surrounded by double quotes.

VAR = "value"

In this example, *VAR* is set to *value*.




  • Early Default Value Assignment (?=)

A variable can be early-assigned a default value using the *?=* assignment operator:

A ?= "1"
B ?= "2"
B ?= "3"

In this example *A* will contain *1* if it was not previously set. *B* will contain *2* because the *?=* assignment operator is immediate meaning that if there are multiple *?=* assignments to a single variable, the first one will be used.




  • Late Default Value Assignment (??=)

A variable can be late-assigned a default value using the *??=* assignment operator:

A ??= "1"
B ??= "2"
B ??= "3"

In this example *A* will contain *1* if it was not previously set. *B* will contain *3* because the *??=* assignment operator is a late or lazy assignment operator and assignment will not take place until the end of the parsing process. If there are multiple *??=* assignments to a single variable, the last one will be used.

A ?= following a ??= will set the variable. Only a = will be override the setting of the ?=. Any following ?= and ??= will not change the setting.




  • Variable Expansion

Bitbake supports referencing a variables content similar to shell scripting.

A = "jumps over"
B = "The quick brown fox ${A} the lazy dog."
echo $B


This results in the the text *The quick brown fox jumps over the lazy dog.* printed to the console.




  • Immediate Variable Expansion (:=)

Typically, Bitbake expands a variable when it is used. The *:=* assignment operator however expands the variable immediately when it is assigned.

A = "11"
B = "B:${A}"
A = "22"
C := "C:${A}"
echo $C $B

This results in *C:22 B:22* printed to the console since the content of * A* is expanded immediately on assignment to variable *C*. Variable *B* did not get expanded until it was actually used in the *echo*statement.




  • Appending (+=) and Prepending (=+)

The *+=* and *=+* append and prepend variables respectively while adding a single space between the values.

A = "12"
A += "34"
B = "89"
B =+ "67"

This results in variable *A* containing *12 34* and variable *B* containing *67 89*.




  • Appending (.=) and Prepending (=.) without Spaces

The *.=* and *=.* work like the above appending and prepending operators however without placing an additional space between the values.

A = "12"
A .= "34"
B = "89"
B =. "67"

This results in variable *A* containing *1234* and variable *B* containing *6789*.