[...] Python will try to convert whatever it actually finds to a Boolean. This lets programmers type
and have it mean "is this variable set to any non-empty string?"
Sort of yes, but no. In fact the line you wrote mean, "has someStringVariable have a value" ; with either False, None and 0 meaning "no value", and everything else meaning a value.
Using this syntax to test a Boolean value come from older languages which don't have a Boolean type. In these languages, 0 mean False and any other value mean True. So, in these language, this line is legit, even if still an error. But with Python, and so Ren'py, it's just an error since Python have a Boolean type.
Code:
$ a = 0
if type( a ) is bool:
pass
else:
"The variable is NOT a boolean."
if not a:
"The test pass"
if a is False:
pass
else:
"It's NOT a false value."
$ a = False
if type( a ) is bool:
"The variable is a boolean."
if not a:
"The test pass"
if a is False:
"It is a false value."
This is one of the pitfalls of "weakly typed languages," of which Python is one.
Sorry, but Python is
not weakly typed. It's a
You must be registered to see the links
language.
The type of each attribute is defined at its creation and can NOT be changed nor seen like being from another type. This is due to the fact that each attribute is in fact an object. When you assign a value to an attribute, Python don't store the value somewhere in memory, but create an object which will handle this value.
Open the console :
In the same time, Python is also dynamically typed ; you don't have to declare the type of the attribute like you usually do with strongly typed language, Python will do it itself.
Open the console :
Code:
a = 0
type( a )
b = "abc"
type( b )
And finally, the attribute are immutable. Each time you change the value of an attribute, whatever by direct assignation ( v = something) or by indirect one (v += 1), you don't update the value, but create a completely new object. "v += 1" doesn't translate to, "add 1 to the value of v", but to, "take the value of v, add 1 then store the result in an attribute named 'v' which will overwrite the previous 'v' attribute".
Open the console :
Code:
a = 0
id( a )
a += 1
id( a )
If you put the dynamically typed and immutability together, it let's Python looks like a weakly typed language, because you have the impression that you can assign a string to what was an integer. But it's all, it "looks like", just looks, not acts. You also can't do what's possible with weakly typed languages, like, by example, taking an array and performing string manipulations on it, or taking an integer and adding a string at the end of it. If you try it, Python will complain :
It will lead to :"TypeError: coercing to Unicode: need string or buffer, int found"