Because if is a keyword. You have similar problems with o.while and o.for :
pax> python >>> class C(object): pass ... >>> o = C() >>> o.not_a_keyword = 123 >>> o.if = 123 File "<stdin>", line 1 o.if = 123 ^ SyntaxError: invalid syntax >>> o.while = 123 File "<stdin>", line 1 o.while = 123 ^ SyntaxError: invalid syntax >>> o.for = 123 File "<stdin>", line 1 o.for = 123 ^ SyntaxError: invalid syntax
Other keywords in Python can be obtained using:
>>> import keyword >>> keyword.kwlist ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
Normally you should not use the keyword as a variable name in Python.
I would suggest choosing a more descriptive name, for example iface , if it is an interface, or infld for an input field, etc.
As for your question about why keywords are not allowed, this greatly simplifies parsers if lexical elements are not contextual. It is necessary to treat the lexical if token as a keyword in some places, and the identifier in others will introduce complexity that is not needed if you choose your identifiers more wisely.
For example, the C ++ operator:
long int int = char[new - int];
can be (with little difficulty) evaluated using a complex parser based on where these lexical elements originate (and what exists on both sides of them). But, at least in part, in the interest of simplicity (and readability) this is not done.