Spaces in object attribute

Emile van Sebille emile at fenx.com
Thu Jan 10 09:59:16 EST 2002


"Alex Martelli" <aleax at aleax.it> wrote in message
news:a1k6sr$jer$1 at serv1.iunet.it...
> "Emile van Sebille" <emile at fenx.com> wrote in message
> news:a1k5nr$qp6bs$1 at ID-11957.news.dfncis.de...
> > Shouldn't this cause an SyntaxError?
> >
> > >>> sys.version
> > '2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)]'
> >
> > >>> class Test:pass
> >
> > >>> obj=Test()
> > >>> 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?
>
> Current Python rule is that whitespace _between_ tokens is always OK -- it
> may be mandatory (when the tokens would otherwise "run into each other"
and
> become a single one -- e.g. "forxin" vs "for x in") or optional
(otherwise),
> but it's never forbidden.  Quite a simple rule.

 [snip]


Well I'll be damned!  Spaces are allowed on either side of the period as
well.

>>> obj . attr  # look! the period binds!
1
>>> obj      .      attr
1

Do people really write programs that use this?  I'm not looking to change
it, just understand why I got bit by it.  ;-)

I did find http://www.python.org/doc/current/ref/delimiters.html and
thinking of a period as a delimiter completes my understanding of this.  I
don't know why I previously assumed differently.  ;-)

Thanks to you and Martin for setting me straight.

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list