Graphics Reference
In-Depth Information
Python has other operators also. It is important to realize when working with operators that they often have
different meanings depending on the data type they are used with. Each data type listed in the previous section
has different properties. One of the ways in which these data types differ is the way they behave with regard to
operators.Agoodexampleofthisisthe + operator.Ifyouusethisoperatorbetweenintegers,asinthisexample,
the predictable result is integer addition:
>>> 5+1
6
>>>
Using the + operator with floating-point numbers is similarly predictable. However, when working on
strings, the meaning of the operator changes. In this case, the + operator becomes the string concatenation op-
erator, as you can see in this example:
>>> "Hello " + "World!"'Hello World!'
>>>
If the + operator is used with lists, as in the following code, it has yet another meaning, namely list concat-
enation, which is to say the same meaning as extend() :
>>> ['a','b','c']+['d','e','f']
['a', 'b', 'c', 'd', 'e', 'f']
If you try to use the + operator with two data types for which it means different things, an error will arise, as
in this case:
>>> "Hello World!" + 5
Traceback (most recent call last):
File "<blender_console>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
Another interesting example of operator behavior is the * operator. For integers and floats, it behaves as an
ordinary multiplication operator. For strings and lists, however, it can be used with an integer to output a string
or list consisting of the original string or list repeated the number of times indicated by the integer argument:
>>> "Hello" * 3
'HelloHelloHello'
>>> ['a','b','c'] * 3
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
Python is full of high-level tricks like this, and it is impossible for this topic to do more than scratch the sur-
face. This much knowledge of operators should be enough to get you going on scripting for Blender.
Variables
Python variables are represented as alphanumeric strings (without quotes, to distinguish them from actual
Python strings). Unlike some languages such as Perl, in which variable names begin with specific characters to
indicatetheirdatatype,variablenamesinPythoncanfreelybeusedforanydatatype.Infact,thesamevariable
can be assigned differently typed values one after another. You can check an item's type by using the type()
function. In the following example, the variable my_variable is initialized with a value of 10 . When the
type is checked, Python reports that the variable is an int . Then a new value of "Hello" is assigned to the
variable and checked again to find that the variable is now of the string type:
Search WWH ::




Custom Search