Spaces in object attribute

Martin von Loewis loewis at informatik.hu-berlin.de
Thu Jan 10 09:38:57 EST 2002


"Emile van Sebille" <emile at fenx.com> writes:

> Shouldn't this cause an SyntaxError?

Why would you think it should?

> >>> obj. attr = 1 # note the space
> >>>
> 
> In particular, I ran across this while printing out debugging info when I
> typoed in a period for a comma.  I was surprised to get an attribute error
> at run time instead of a syntax error at compile time.
> 
> Is there a reason to allow spaces like this?

Certainly. A programming language program consists of a sequence of
tokens. It is common to allow arbitrary space between tokens. In
Python, 'obj', '.', 'attr', and '=' are all separate tokens. The only
case where space is not allowed is inside tokens themselves, like

  o bj.a ttr=1

There are also cases where space between tokens is mandatory, such as
when separating keywords and identifiers.

Regards,
Martin



More information about the Python-list mailing list