[Tutor] datetime syntax error for May 8th and 9th 2008??

Che M pine508 at hotmail.com
Tue May 20 09:01:50 CEST 2008




> Date: Fri, 16 May 2008 23:38:42 -0700
> From: wescpy at gmail.com
> To: pine508 at hotmail.com; john at fouhy.net
> Subject: Re: [Tutor] datetime syntax error for May 8th and 9th 2008??
> CC: tutor at python.org
> 
> On Fri, May 16, 2008 at 9:29 PM, John Fouhy <john at fouhy.net> wrote:
> > On 17/05/2008, Che M <pine508 at hotmail.com> wrote:
> >> >>> datetime.datetime(2008, 05, 08)
> >> SyntaxError: invalid token
> >
> > It's simpler than that... Try this:
> >
> >>>> x = 08
> >  File "<stdin>", line 1
> >    x = 08
> >         ^
> > SyntaxError: invalid token
> >>>> x = 010
> >>>> x
> > 8
> >
> > Basically, python interprets integer literals starting with 0 as octal
> > numbers.  It's an old convention from C (or earlier?).  It doesn't
> > affect strings, so int('010') == 10 (unless you use eval).
> 
> 
> che,
> 
> john is correct.  any leading zero (0) of an integer number will be
> translated as base 8 or octal, which counts by 8's:
> 
> 00, 01, 02, 03, 04, 05, 06, 07, 10 (decimal/base 10: 8*1+0=8),
> 11 (decimal: 8*1+1=9), 12 (decimal: 8*1+2=10), 13 (decimal 8+3=11),
> 14, 15, 16, 17 (decimal: 15), 20 (decimal: 8*2+0=16), 21 (decimal:
> 8*2+1=17), etc...
> 
> the problem with "08" is that because you're counting by 8s, a value
> of "08" is invalid because the digits "8" and "9" are not part of the
> octal "alphabet" or character set. that is why "07" works but not
> "08".
> 
> as an FYI, because of this confusion, starting with Python 3.0, octal
> numbers will require a lowercase "o" (in the same manner as
> hexadecimal/base 16 numbers require an "x") after leading 0, i.e.,
> 0o7, 0o16, etc.  (Oo8 is still an invalid octal number.)
> 
> hope this helps!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Thank you, Wesley, that is helpful and interesting.  
Best regards,
Che

_________________________________________________________________
E-mail for the greater good. Join the i’m Initiative from Microsoft.
http://im.live.com/Messenger/IM/Join/Default.aspx?source=EML_WL_ GreaterGood
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080520/d8e17372/attachment.htm>


More information about the Tutor mailing list