Prev: Unicode strings
Next: exception in Tkinter on Ubtunu...
From: Jussi Piitulainen on 12 Feb 2010 12:48 Terry Reedy writes: > On 2/12/2010 4:40 AM, Jussi Piitulainen wrote: > > Terry Reedy writes: > >> On 2/11/2010 11:23 AM, Jussi Piitulainen wrote: > >>> Robert Kern writes: > >>>> On 2010-02-11 06:31 AM, Shashwat Anand wrote: > >>>>> There is a little issue here that '>>> -.1 ** .1' should give you > >>>>> error message. That is it. > >>>> > >>>> No, fractional powers of negative numbers are perfectly valid > >>>> mathematically. The result is a complex number. In Python 3 (what > >>>> the OP is using), this has been implemented, but not in Python 2.6. > >>> > >>> Perhaps it should raise a MisleadingSpacingError at compile time. > >> > >> You forgot the smiley ;-). > >> > >> > The error message could recommend - .1**.1, or better -(.1 ** .1). > >> > >> The compiler would never see the difference between -.1 ** .1 and the > >> first and probably no difference with the second either. > > > > It could be done the same way that the compiler keeps track of the > > source line numbers now: early phases annotate their output with any > > information that later phases may need. This error would be detected > > during syntactic analysis. > > There is no error to detect. Sorry, read the manual and either learn > or lookup precedence rules (there is a table in the end of the > Expressions chapter) or use parentheses when not sure. Thanks.
From: Albert van der Horst on 22 Feb 2010 13:01 In article <mailman.2359.1265890457.28905.python-list(a)python.org>, Terrence Cole <terrence(a)zettabytestorage.com> wrote: >Can someone explain to me what python is doing here? > >Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47) >[GCC 4.3.4] on linux2 >Type "help", "copyright", "credits" or "license" for more information. >>>> -0.1 ** 0.1 Python 4.0 Warning: misleading blank space, expected: - 0.1**0.1 >-0.7943282347242815 >>>> a = -0.1; b = 0.1 >>>> a ** b >(0.7554510437117542+0.2454609236416552j) >>>> -abs(a ** b) >-0.7943282347242815 > >Why does the literal version return the signed magnitude and the >variable version return a complex? > >Cheers, >Terrence > -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert(a)spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
From: Steven D'Aprano on 23 Feb 2010 03:11 On Mon, 22 Feb 2010 18:01:44 +0000, Albert van der Horst wrote: > In article <mailman.2359.1265890457.28905.python-list(a)python.org>, > Terrence Cole <terrence(a)zettabytestorage.com> wrote: >>Can someone explain to me what python is doing here? >> >>Python 3.1.1 (r311:74480, Feb 3 2010, 13:36:47) [GCC 4.3.4] on linux2 >>Type "help", "copyright", "credits" or "license" for more information. >>>>> -0.1 ** 0.1 > > Python 4.0 > Warning: misleading blank space, expected: > - 0.1**0.1 > >>-0.7943282347242815 Making spaces significant in that fashion is mind-bogglingly awful. Let's look at a language that does this: [steve(a)sylar ~]$ cat ws-example.rb def a(x=4) x+2 end b = 1 print (a + b), (a+b), (a+ b), (a +b), "\n" [steve(a)sylar ~]$ ruby ws-example.rb 7773 -- Steven
From: Mark Dickinson on 23 Feb 2010 08:48 On Feb 23, 8:11 am, Steven D'Aprano <ste...(a)REMOVE.THIS.cybersource.com.au> wrote: > Making spaces significant in that fashion is mind-bogglingly awful. Let's > look at a language that does this: > > [steve(a)sylar ~]$ cat ws-example.rb > def a(x=4) > x+2 > end > > b = 1 > print (a + b), (a+b), (a+ b), (a +b), "\n" > > [steve(a)sylar ~]$ ruby ws-example.rb > 7773 Hmm. That's pretty nasty, all right. Not that Python can claim to be immune to such behaviour: >>> 3 .real 3 >>> 3. real File "<stdin>", line 1 3. real ^ SyntaxError: invalid syntax Though the fact that one of the cases raises an exception (rather than silently giving some different behaviour) ameliorates things a bit. -- Mark
From: Steven D'Aprano on 23 Feb 2010 20:15 On Tue, 23 Feb 2010 05:48:09 -0800, Mark Dickinson wrote: > On Feb 23, 8:11 am, Steven D'Aprano > <ste...(a)REMOVE.THIS.cybersource.com.au> wrote: >> Making spaces significant in that fashion is mind-bogglingly awful. >> Let's look at a language that does this: >> >> [steve(a)sylar ~]$ cat ws-example.rb >> def a(x=4) >> x+2 >> end >> >> b = 1 >> print (a + b), (a+b), (a+ b), (a +b), "\n" >> >> [steve(a)sylar ~]$ ruby ws-example.rb >> 7773 > > Hmm. That's pretty nasty, all right. Not that Python can claim to be > immune to such behaviour: > >>>> 3 .real > 3 >>>> 3. real > File "<stdin>", line 1 > 3. real > ^ > SyntaxError: invalid syntax > > > Though the fact that one of the cases raises an exception (rather than > silently giving some different behaviour) ameliorates things a bit. It ameliorates it *completely* -- you won't get silent errors in Python because you add or delete whitespace around a dot. "I find it amusing when novice programmers believe their main job is preventing programs from crashing. ... More experienced programmers realize that correct code is great, code that crashes could use improvement, but incorrect code that doesn't crash is a horrible nightmare." http://www.pphsg.org/cdsmith/types.html The edge case occurs because dot does double-duty as an operator and as part of float literals. However, float literals never include whitespace: >>> 1.5 1.5 >>> 1 . 5 File "<stdin>", line 1 1 . 5 ^ SyntaxError: invalid syntax and likewise for 1. 5 and 1 .5 -- the only way to get a float literal with a decimal point is by not including whitespace in it. So there is never any ambiguity about floats. You can even do this: >>> 1.5.__str__() '1.5' And since . is an operator outside of float literals, you can do this: >>> import sys >>> sys . platform 'linux2' although why you'd want to escapes me :) This actually is a feature, since it is useful when calling methods on int literals. However this is a very rare thing to do. -- Steven
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Unicode strings Next: exception in Tkinter on Ubtunu... |