"raise" followed by conditional statement (python)

  • Last Update :
  • Techknowledgy :

What they should have written is:

assert isinstance(matrix, ButtonMatrixElement)

It appears you found decompiled Ableton Live scripts, but the decompilation script produced wrong Python code. The bytecode for an assert looks like this (Python 2.5 bytecode):

>>> import dis
>>> dis.dis(compile('''assert isinstance(matrix, ButtonMatrixElement)''', '<stdin>', 'exec'))
   1 0 LOAD_NAME 0 (isinstance)
   3 LOAD_NAME 1 (matrix)
   6 LOAD_NAME 2 (ButtonMatrixElement)
   9 CALL_FUNCTION 2
   12 JUMP_IF_TRUE 7 (to 22)
   15 POP_TOP
   16 LOAD_GLOBAL 3 (AssertionError)
   19 RAISE_VARARGS 1
   >> 22 POP_TOP
   23 LOAD_CONST 0 (None)
   26 RETURN_VALUE

Note however that if the isinstance() call returns True, the jump instruction (index 12, JUMP_IF_TRUE) jumps past the RAISE_VARARGS instruction, while the re-constructed code doesn't. Compare this to an actual raise ... or ... statement, you'll notice the jump doesn't go past the raise:

>>> dis.dis(compile('raise foo or bar', '<stdin>', 'exec'))
   1 0 LOAD_NAME 0 (foo)
   3 JUMP_IF_TRUE 4 (to 10)
   6 POP_TOP
   7 LOAD_NAME 1 (bar)
   >> 10 RAISE_VARARGS 1
   13 LOAD_CONST 0 (None)
   16 RETURN_VALUE

The aforementioned bug should be fixed there. When you decompile MainSelectorComponent.pyc with this version, you get:

class MainSelectorComponent(ModeSelectorComponent):
   ""
" Class that reassigns the button on the launchpad to different functions "
""

def __init__(self, matrix, top_buttons, side_buttons, config_button):
   assert isinstance(matrix, ButtonMatrixElement)
assert matrix.width() == 8 and matrix.height() == 8
assert isinstance(top_buttons, tuple)
assert len(top_buttons) == 8
assert isinstance(side_buttons, tuple)
assert len(side_buttons) == 8
assert isinstance(config_button, ButtonElement)

Suggestion : 2

The following is the logical flow for matching a class pattern against a subject value:,The following is the logical flow for matching a sequence pattern against a subject value:,The following is the logical flow for matching a mapping pattern against a subject value:,If name_or_attr is not an instance of the builtin type , raise TypeError.

if test1: if test2: print(x)
if x < y < z: print(x);
print(y);
print(z)

The if statement is used for conditional execution:

if_stmt:: = "if"
assignment_expression ":"
suite
   ("elif"
      assignment_expression ":"
      suite) * ["else"
      ":"
      suite
   ]

The while statement is used for repeated execution as long as an expression is true:

while_stmt:: = "while"
assignment_expression ":"
suite
   ["else"
      ":"
      suite]

The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object:

for_stmt:: = "for"
target_list "in"
expression_list ":"
suite
   ["else"
      ":"
      suite]
2._
for i in range(10):
   print(i)
i = 5 # this will not affect the
for -loop
# because i will be overwritten with the next
# index in the range
for i in range(10):
   print(i)
i = 5 # this will not affect the
for -loop
# because i will be overwritten with the next
# index in the range

Suggestion : 3

Above, the if condition contains multiple statements with the same indentation. If all the statements are not in the same indentation, either space or a tab then it will raise an IdentationError.,All the if, elif, and else conditions must start from the same indentation level, otherwise it will raise the IndentationError.,In the above example, the if condition price >= 100 is False, so the else block will be executed. The else block can also contain multiple statements with the same indentation; otherwise, it will raise the IndentationError.,Notice that each if block contains a statement in a different indentation, and that's valid because they are different from each other.

if [boolean expression]:
statement1
statement2
   ...
   statementN
price = 50

if price < 100:
   print("price is less than 100")
price is less than 100
price = 50
quantity = 5
if price * quantity < 500:
   print("price*quantity is less than 500")
print("price = ", price)
print("quantity = ", quantity)
price * quantity is less than 500
price = 50
quantity = 5
price = 50
quantity = 5
if price * quantity < 500:
   print("price is less than 500")
print("price = ", price)
print("quantity = ", quantity)

Suggestion : 4

fails to account for the fact that expressions are always evaluated from left to right. That is, bool(x.isupper()) will always be evaluated first in this instance and will raise an error if x is not a string. Thus the following isinstance(x, str) statement is useless.,is problematic because isupper can only be called once we are sure that x is a string; this code will raise an error if x is a number. We could instead write,See, that if x is not a string, that isinstance(x, str) will return False; thus isinstance(x, str) and x.isupper() will short-circuit and return False without ever evaluating bool(x.isupper()). This is the preferable way to handle this sort of checking. This code is more concise and readable than the equivalent nested if-statements.,Reading Comprehension Exercise Solutions:

# a simple
if -
else block
if x < 0:
   x = x ** 2
else:
   x = x ** 3
>>> 2 < 3
True
>>> x = 3 # assign the value 3 to the variable `x` >>>
   x == 3 # check
if `x`
and 3 have the same value
True
>>> 2 < 3 < 1 # performs(2 < 3) and(3 < 1)
False
# demonstrating `==`
vs `is` >>>
   x = [1, 2, 3] >>>
   y = [1, 2, 3]

   >>>
   x == y
True

# `x`
and `y`
reference equivalent, but distinct lists
   >>>
   x is y
False
>>> x = None >>>
   x is None
True

#(2 < 0) returns the object `False`
# thus this becomes: `False is False` >>>
   (2 < 0) is False
True