Re: Overriding True and False ?

2017-01-29 Thread INADA Naoki
It's fixed already in Python 3.
Please use Python 3 when teaching to students.

$ python3
Python 3.6.0 (default, Dec 24 2016, 00:01:50)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> True = "foo"
  File "", line 1
SyntaxError: can't assign to keyword
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Overriding True and False ?

2017-01-29 Thread Deborah Swanson
Irv Kalb wrote, on Sunday, January 29, 2017 9:04 PM
> 
> I teach intro to programming using Python.  In my first 
> assignment, students are asked to assign variables of 
> different types and print out the values.  
> 
> One student (who really did not understand Booleans) turned 
> in the following for his/her interpretation of Booleans (Python 2.7):
> 
> True = 'shadow'
> False = 'light'
> print "If the sun is behind a cloud, there is", True
> print "If it is a clear day, there is", False
> 
> And it printed:
> 
> If the sun is behind a cloud, there is shadow
> If it is a clear day, there is light
> 
> 
> It seems very odd that Python allows you to override the 
> values of True and False.  In the code, True and False were 
> clearly recognized as keywords as they were colored purple.  
> But there was no error message.
> 
> You cannot assign new values to other keywords.  Simple tests 
> of things like:
> 
> for = 5
> 
> while = 2
> 
> not = 3
> 
> As expected, all result in SyntaxError: invalid syntax.  Why 
> would Python allow you to override the values of True and 
> False?  I wonder if this is some sort of historical thing as 
> these are the only keywords besides None that are uppercased. 
>  This line:
> 
> None = 5
> 
> Even gives a special SyntaxError: cannot assign to None
> 
> Just curious,
> 
> Irv

Just guessing, but in the examples you give in Python 2.7, substitute
strings are syntactically correct in print statements, but:

5 in list('abc'):

2 True:

if a 3 b:

would all be syntactical errors.

As is 'None = 5'.

Looks like the moral of the story is that in Python 2.7 you can redefine
keywords, so long as you don't get any syntax errors after (or during)
redefinition.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-29 Thread Ben Finney
Irv Kalb  writes:

> I teach intro to programming using Python. […]

Thank you for teaching Python to beginners!

> It seems very odd that Python allows you to override the values of
> True and False.

Yes, it is. That's why Python 3 forbids it::

>>> True = "shadow"
  File "", line 1
SyntaxError: can't assign to keyword
>>> False = "light"
  File "", line 1
SyntaxError: can't assign to keyword

When teaching Python, please do not teach Python 2. Your students should
learn Python 3 first, primarily, and for most of the course.

Python 2 is a legacy that will never gain new features, and will only
slip further behind the current supported Python version. That makes
Python 2 a poor choice for teaching to beginners.

-- 
 \  “The entertainment industry calls DRM "security" software, |
  `\ because it makes them secure from their customers.” —Cory |
_o__) Doctorow, 2014-02-05 |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-30 Thread Chris Angelico
On Mon, Jan 30, 2017 at 6:50 PM, Deborah Swanson
 wrote:
> Looks like the moral of the story is that in Python 2.7 you can redefine
> keywords, so long as you don't get any syntax errors after (or during)
> redefinition.

The moral is actually that "True" and "False" aren't keywords in Py2.
They're just built-ins. Compare:

rosuav@sikorsky:~$ python3
Python 3.7.0a0 (default:cebc9c7ad195, Jan 24 2017, 06:55:19)
[GCC 6.2.0 20161027] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> dis.dis(lambda: True)
  1   0 LOAD_CONST   1 (True)
  2 RETURN_VALUE
>>>
rosuav@sikorsky:~$ python
Python 2.7.12+ (default, Sep  1 2016, 20:27:38)
[GCC 6.2.0 20160927] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> dis.dis(lambda: True)
  1   0 LOAD_GLOBAL  0 (True)
  3 RETURN_VALUE
>>>

In Python 2, returning "True" involves a name lookup; in Python 3,
it's simply a constant - a literal.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-30 Thread Chris Angelico
On Mon, Jan 30, 2017 at 4:03 PM, Irv Kalb  wrote:
> It seems very odd that Python allows you to override the values of True and 
> False.  In the code, True and False were clearly recognized as keywords as 
> they were colored purple.  But there was no error message.
>
> You cannot assign new values to other keywords.  Simple tests of things like:
>
> for = 5
>
> while = 2
>
> not = 3
>
> As expected, all result in SyntaxError: invalid syntax.  Why would Python 
> allow you to override the values of True and False?  I wonder if this is some 
> sort of historical thing as these are the only keywords besides None that are 
> uppercased.  This line:
>
> None = 5
>
> Even gives a special SyntaxError: cannot assign to None

There are slightly different things going on here. Trying to assign to
a piece of syntax like "while" makes absolutely no sense, but trying
to assign to "None" is structurally sane, yet disallowed. IIRC there's
only one non-assignable name in Python 2 (None), but as mentioned,
Python 3 adds True and False to that. (Interestingly, Ellipsis is not
included in that.)

> I teach intro to programming using Python.

May I please request that you consider teaching Python 3? Python 2
isn't going anywhere (for better or for worse), and Py3 is a superior
language in many ways, not least of which is that it keeps text and
bytes separate, giving text the full power that it should have. For a
beginning programmer, this is very helpful; there's nothing to
un-learn when going international. (There will be new nuances to be
learned, such as RTL text, but nothing to unlearn.) Python 3 also
fixes a number of other problems that Python 2 inherited from C,
making it altogether a better language for teaching with.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-30 Thread Ian Kelly
On Jan 30, 2017 2:00 AM, "Chris Angelico"  wrote:

(Interestingly, Ellipsis is not
included in that.)


Perhaps because it's rather unusual for a program to depend upon the value
of Ellipsis.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-30 Thread Ian Kelly
On Jan 30, 2017 1:32 AM, "Irv Kalb"  wrote:

I teach intro to programming using Python.  In my first assignment,
students are asked to assign variables of different types and print out the
values.

One student (who really did not understand Booleans) turned in the
following for his/her interpretation of Booleans (Python 2.7):

True = 'shadow'
False = 'light'
print "If the sun is behind a cloud, there is", True
print "If it is a clear day, there is", False

And it printed:

If the sun is behind a cloud, there is shadow
If it is a clear day, there is light


It seems very odd that Python allows you to override the values of True and
False.  In the code, True and False were clearly recognized as keywords as
they were colored purple.  But there was no error message.


Plenty of people have remarked that this is fixed in Python 3, but nobody
that I see has explained yet the reason for this behavior in the first
place. True and False are not keywords in Python 2 for backward
compatibility. Many versions ago, Python did not define True and False at
all. It was common in that era to lead scripts with:

False = 0
True = 1

To define useful boolean constants. When the built-in constants were added,
the developers did not want to break programs that followed this pattern.

This is also the reason why bool is a subclass of int -- so that False == 0
and True == 1 remain true for programs that depend on this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-30 Thread Steve D'Aprano
On Tue, 31 Jan 2017 03:12 am, Ian Kelly wrote:

> On Jan 30, 2017 1:32 AM, "Irv Kalb"  wrote:
> 
> I teach intro to programming using Python.  In my first assignment,
> students are asked to assign variables of different types and print out
> the values.
[...]

Hey Ian,

Your news reader or mail client has stopped quoting the text you are
quoting, so it appears as if you have written it.

See:

https://mail.python.org/pipermail/python-list/2017-January/719015.html



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-30 Thread Ian Kelly
On Jan 30, 2017 6:07 PM, "Steve D'Aprano" 
wrote:
> Hey Ian,
>
> Your news reader or mail client has stopped quoting the text you are
> quoting, so it appears as if you have written it.
>
> See:
>
> https://mail.python.org/pipermail/python-list/2017-January/719015.html

Well, nuts. It looks fine in my client (the Gmail Android app) so I guess
this must be an issue of HTML versus text content. Unfortunately the mobile
client doesn't have any option for text-only that I can find, so my options
appear to be to laboriously replace the quoting as I've done here, or just
stop using the mobile client.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-30 Thread Ben Finney
Ian Kelly  writes:

> Well, nuts. It looks fine in my client (the Gmail Android app) so I
> guess this must be an issue of HTML versus text content. Unfortunately
> the mobile client doesn't have any option for text-only that I can
> find, so my options appear to be to laboriously replace the quoting as
> I've done here, or just stop using the mobile client.

For composing discussion posts, yes, I recommend people stop using
mobile clients. It can wait until you're at a real keyboard and a decent
authoring environment.

-- 
 \  “Very few things happen at the right time, and the rest do not |
  `\ happen at all. The conscientious historian will correct these |
_o__)  defects.” —Mark Twain, _A Horse's Tale_ |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-30 Thread Christian Gollwitzer

Am 31.01.17 um 04:27 schrieb Ian Kelly:

On Jan 30, 2017 6:07 PM, "Steve D'Aprano" 
wrote:

Hey Ian,

Your news reader or mail client has stopped quoting the text you are
quoting, so it appears as if you have written it.

See:

https://mail.python.org/pipermail/python-list/2017-January/719015.html


Well, nuts. It looks fine in my client (the Gmail Android app) so I guess
this must be an issue of HTML versus text content. Unfortunately the mobile
client doesn't have any option for text-only that I can find, so my options
appear to be to laboriously replace the quoting as I've done here, or just
stop using the mobile client.


This one looks fine, FYI.

Christian
--
https://mail.python.org/mailman/listinfo/python-list


Re: Overriding True and False ?

2017-01-31 Thread jladasky
On Sunday, January 29, 2017 at 11:29:29 PM UTC-8, Irv Kalb wrote:

> It seems very odd that Python allows you to override the values of 
> True and False.

Hi Irv,

Let me join the chorus of people who are encouraging you to switch to Python3, 
which corrects this problem.  Overriding builtins has been judged harmful, and 
now it is a lot harder to do.

There are sometimes good reasons for shadowing builtins, but more than a few 
newcomers to Python (once upon a time, myself included) wrote buggy code by 
accident because of this surprising flexibility.

That being said, I can't resist re-posting a story from 14 years ago:

https://groups.google.com/forum/#!original/comp.lang.python/xEtUYsxLnFE/6yYgvhvkggMJ

==

From: lad...@my-deja.com (John Ladasky)
Newsgroups: comp.lang.python
Subject: Re: Python 2.3 True = False
Date: 24 Mar 2003 12:29:04 -0800

an...@calbay.com (Anand) wrote in message 
news:...
> >>> True = False
> >>> True
>  False

Shhh!  Anand!  You're not supposed to show anyone that!  Now every
politician on Earth will want to program in Python!

==
-- 
https://mail.python.org/mailman/listinfo/python-list