Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52914 > unrolled thread

Setting the value of True

Started byjeangawron@gmail.com
First post2013-08-23 16:38 -0700
Last post2013-08-25 13:27 +1200
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Setting the value of True jeangawron@gmail.com - 2013-08-23 16:38 -0700
    Re: Setting the value of True Gary Herron <gary.herron@islandtraining.com> - 2013-08-23 16:48 -0700
    Re: Setting the value of True Terry Reedy <tjreedy@udel.edu> - 2013-08-23 19:56 -0400
    Re: Setting the value of True Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-24 01:14 +0000
      Re: Setting the value of True Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-08-25 13:27 +1200

#52914 — Setting the value of True

Fromjeangawron@gmail.com
Date2013-08-23 16:38 -0700
SubjectSetting the value of True
Message-ID<9da851be-9ab3-4984-8f3e-71582f8ff4da@googlegroups.com>
Python allows you set the value of True

>>> True = 1.3

Now this is consistent with the decision to let you set the
value of various builtin names.  But why is this case different:

>>> None = 1.3
  File "<stdin>", line 1
SyntaxError: cannot assign to None

Mark Gawron

[toc] | [next] | [standalone]


#52916

FromGary Herron <gary.herron@islandtraining.com>
Date2013-08-23 16:48 -0700
Message-ID<mailman.183.1377302041.19984.python-list@python.org>
In reply to#52914
On 08/23/2013 04:38 PM, jeangawron@gmail.com wrote:
> Python allows you set the value of True
>
>>>> True = 1.3
> Now this is consistent with the decision to let you set the
> value of various builtin names.  But why is this case different:
>
>>>> None = 1.3
>    File "<stdin>", line 1
> SyntaxError: cannot assign to None
>
> Mark Gawron

Python3 fixes this inconsistency, by disallowing all such assignments.

They all raise an exception:
       SyntaxError: assignment to keyword

[toc] | [prev] | [next] | [standalone]


#52917

FromTerry Reedy <tjreedy@udel.edu>
Date2013-08-23 19:56 -0400
Message-ID<mailman.184.1377302205.19984.python-list@python.org>
In reply to#52914
On 8/23/2013 7:38 PM, jeangawron@gmail.com wrote:
> Python allows you set the value of True

Unqualified 'Python', used in the present tense, refers to the latest 
release or repository version.

 >>> True = 1.3
SyntaxError: assignment to keyword

>>>> True = 1.3
>
> Now this is consistent with the decision to let you set the
> value of various builtin names.  But why is this case different:
>
>>>> None = 1.3
>    File "<stdin>", line 1
> SyntaxError: cannot assign to None


-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#52920

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-08-24 01:14 +0000
Message-ID<521808ec$0$29986$c3e8da3$5496439d@news.astraweb.com>
In reply to#52914
On Fri, 23 Aug 2013 16:38:43 -0700, jeangawron wrote:

> Python allows you set the value of True
> 
>>>> True = 1.3

Only in older versions of Python. This is for historical reasons: True 
and False were added as built-ins relatively late in Python's history 
(2.2, I think) and so there is still old code that legitimately does this 
at the start of a module:

True = 1
False = not True

Hence, making True and False actual keywords could only occur in Python 
3, which was allowed to break backwards compatibility.

None, on the other hand, has always existed in Python. The very oldest 
versions way back in the mists of time allowed you to rebind None:

[steve@ando ~]$ python1.5
Python 1.5.2 (#1, Aug 27 2012, 09:09:18)  [GCC 4.1.2 20080704 (Red Hat 
4.1.2-52)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> None = 3
>>> None
3


but as that was considered a mistake, *and* it was considered unlikely to 
be any code in the wild that legitimately rebinds None, that was made a 
SyntaxError in Python 2.x.

As for why None, True and False are treated differently than built-ins, 
if I remember the reason why, it is because they are considered 
fundamental to the inner workings of Python, unlike mere builtins like 
len, map, etc. and therefore should be protected.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#52960

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2013-08-25 13:27 +1200
Message-ID<b7t4r6Fd6ufU1@mid.individual.net>
In reply to#52920
Steven D'Aprano wrote:
> As for why None, True and False are treated differently than built-ins, 
> if I remember the reason why, it is because they are considered 
> fundamental to the inner workings of Python, unlike mere builtins like 
> len, map, etc. and therefore should be protected.

It's probably more to permit optimisation. Treating them
as constants avoids a name lookup every time they're used.

-- 
Greg

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web