Why do integers compare equal to booleans?

2018-11-16 Thread Steve Keller
Why do the integers 0 and 1 compare equal to the boolean values False
and True and all other integers to neither of them?

$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 0 == False
True
>>> 1 == True
True
>>> 2 == False
False
>>> 2 == True
False
>>> -1 == False
False
>>> -1 == True
False
>>>

Since these are objects of different types I would expect they cannot
be equal.  I know that 0 means false and != 0 means true in C, C++,
etc. but in Python that surprises me.

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


Generators, generator expressions, and loops

2018-11-16 Thread Steve Keller
I have looked at generators, generator expressions, and iterators and
I try to get more familiar with these.

1. How would I loop over all (with no upper bound) integers or all
powers of two, for example?

In C it would be

   for (int i = 0; ; i++) { ... }  or  for (int i = 1; ; i *= 2) { ... }

In Python, I could define a generator

def powers():
i = 1
while True:
yield(i)
i *= 2

for i in powers():
...

More elegant are generator expressions but I cannot think of a way
without giving an upper limit:

for i in (2 ** i for i in range(100)):
...

which looks ugly.  Also, the double for-loop (and also the two loops
in the above exmaple, for + while in the generator) look unnatural,
somehow, i.e. loop over all elements which are created by a loop.

Is there a more beautyful way?

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


Generators, generator expressions, and loops

2018-11-16 Thread Steve Keller
I have looked at generators, generator expressions, and iterators and
I try to get more familiar with these.

1. How would I loop over all (with no upper bound) integers or all
powers of two, for example?

In C it would be

   for (int i = 0; ; i++) { ... }  or  for (int i = 1; ; i *= 2) { ... }

In Python, I could define a generator

def powers():
i = 1
while True:
yield(i)
i *= 2

for i in powers():
...

More elegant are generator expressions but I cannot think of a way
without giving an upper limit:

for i in (2 ** i for i in range(100)):
...

which looks ugly.  Also, the double for-loop (and also the two loops
in the above exmaple, for + while in the generator) look unnatural,
somehow, i.e. loop over all elements which are created by a loop.

Is there a more beautyful way?

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


Iterators of iterators

2018-11-16 Thread Steve Keller
I wonder why iterators do have an __iter__() method?  I thought
iterable objects would have an __iter__() method (but no __next__())
to create an iterator for it, and that would have the __next__()
method but no __iter__().

$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> l = [1,2,3]
>>> next(l)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'list' object is not an iterator

This is expected, of course.

>>> iter(l)

>>> iter(iter(l))

>>> iter(iter(iter(l)))

>>> i = iter(iter(iter(l)))
>>> list(i)
[1, 2, 3]

Is there any reason or usage for this?

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


Generators, generator expressions, and loops

2018-11-16 Thread Steve Keller
Cancel ill-formated article
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why do integers compare equal to booleans?

2018-11-16 Thread Jon Ribbens
On 2018-11-16, Steve Keller  wrote:
> Why do the integers 0 and 1 compare equal to the boolean values False
> and True and all other integers to neither of them?

Because Python used not to have a boolean type and used the integers
0 and 1 instead, so when the boolean type was introduced True and
False were made to behave very much like 1 and 0 for backwards
compatibility reasons.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why do integers compare equal to booleans?

2018-11-16 Thread duncan smith
On 16/11/18 14:51, Steve Keller wrote:
> Why do the integers 0 and 1 compare equal to the boolean values False
> and True and all other integers to neither of them?
> 
> $ python3
> Python 3.5.2 (default, Nov 12 2018, 13:43:14)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> 0 == False
> True
> >>> 1 == True
> True
> >>> 2 == False
> False
> >>> 2 == True
> False
> >>> -1 == False
> False
> >>> -1 == True
> False
> >>>
> 
> Since these are objects of different types I would expect they cannot
> be equal.  I know that 0 means false and != 0 means true in C, C++,
> etc. but in Python that surprises me.
> 
> Steve
> 

>>> isinstance(False, int)
True
>>> isinstance(True, int)
True
>>> False.real
0
>>> True.real
1
>>>

At least in recent Pythons.

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


RE: Why do integers compare equal to booleans?

2018-11-16 Thread David Raymond
A boolean type didn't come about until version 2.3, and even now they still 
inherit from integers.

Some links for you:

https://docs.python.org/3.7/whatsnew/2.3.html#pep-285-a-boolean-type
https://docs.python.org/3.7/library/stdtypes.html#boolean-values
https://docs.python.org/3.7/reference/datamodel.html#the-standard-type-hierarchy



-Original Message-
From: Python-list 
[mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of 
Steve Keller
Sent: Friday, November 16, 2018 9:51 AM
To: python-list@python.org
Subject: Why do integers compare equal to booleans?

Why do the integers 0 and 1 compare equal to the boolean values False
and True and all other integers to neither of them?

$ python3
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 0 == False
True
>>> 1 == True
True
>>> 2 == False
False
>>> 2 == True
False
>>> -1 == False
False
>>> -1 == True
False
>>>

Since these are objects of different types I would expect they cannot
be equal.  I know that 0 means false and != 0 means true in C, C++,
etc. but in Python that surprises me.

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


Re: Iterators of iterators

2018-11-16 Thread Ian Kelly
On Fri, Nov 16, 2018 at 8:01 AM Steve Keller  wrote:
>
> I wonder why iterators do have an __iter__() method?  I thought
> iterable objects would have an __iter__() method (but no __next__())
> to create an iterator for it, and that would have the __next__()
> method but no __iter__().
>
> $ python3
> Python 3.5.2 (default, Nov 12 2018, 13:43:14)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> l = [1,2,3]
> >>> next(l)
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: 'list' object is not an iterator
>
> This is expected, of course.
>
> >>> iter(l)
> 
> >>> iter(iter(l))
> 
> >>> iter(iter(iter(l)))
> 
> >>> i = iter(iter(iter(l)))
> >>> list(i)
> [1, 2, 3]
>
> Is there any reason or usage for this?

Iterators are required to have an __iter__ method that just returns
self. The reason is so that iterators can be used in places where an
iterable is required; e.g. so that code that is handed an iterator can
loop over it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generators, generator expressions, and loops

2018-11-16 Thread Ian Kelly
On Fri, Nov 16, 2018 at 7:57 AM Steve Keller  wrote:
>
> I have looked at generators, generator expressions, and iterators and
> I try to get more familiar with these.
>
> 1. How would I loop over all (with no upper bound) integers or all
> powers of two, for example?
>
> In C it would be
>
>for (int i = 0; ; i++) { ... }  or  for (int i = 1; ; i *= 2) { ... }
>
> In Python, I could define a generator
>
> def powers():
> i = 1
> while True:
> yield(i)
> i *= 2
>
> for i in powers():
> ...
>
> More elegant are generator expressions but I cannot think of a way
> without giving an upper limit:
>
> for i in (2 ** i for i in range(100)):
> ...
>
> which looks ugly.  Also, the double for-loop (and also the two loops
> in the above exmaple, for + while in the generator) look unnatural,
> somehow, i.e. loop over all elements which are created by a loop.
>
> Is there a more beautyful way?

Some options:

from itertools import count

def powers():
for i in count():
yield 2 ** i


for i in (2 ** i for i in count()):
...


for i in map(lambda x: 2 ** x, count()):
...


from functools import partial
from operator import pow

for i in map(partial(pow, 2), count()):
...


Take your pick.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why do integers compare equal to booleans?

2018-11-16 Thread Santiago Basulto
> Because Python used not to have a boolean type and used the integers 0
and 1 instead

Exactly as Jon says. I wrote a post some time ago with more info about it:
https://blog.rmotr.com/those-tricky-python-booleans-2100d5df92c

On Fri, Nov 16, 2018 at 12:23 PM duncan smith 
wrote:

> On 16/11/18 14:51, Steve Keller wrote:
> > Why do the integers 0 and 1 compare equal to the boolean values False
> > and True and all other integers to neither of them?
> >
> > $ python3
> > Python 3.5.2 (default, Nov 12 2018, 13:43:14)
> > [GCC 5.4.0 20160609] on linux
> > Type "help", "copyright", "credits" or "license" for more
> information.
> > >>> 0 == False
> > True
> > >>> 1 == True
> > True
> > >>> 2 == False
> > False
> > >>> 2 == True
> > False
> > >>> -1 == False
> > False
> > >>> -1 == True
> > False
> > >>>
> >
> > Since these are objects of different types I would expect they cannot
> > be equal.  I know that 0 means false and != 0 means true in C, C++,
> > etc. but in Python that surprises me.
> >
> > Steve
> >
>
> >>> isinstance(False, int)
> True
> >>> isinstance(True, int)
> True
> >>> False.real
> 0
> >>> True.real
> 1
> >>>
>
> At least in recent Pythons.
>
> Duncan
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 
Santiago Basulto.-
Co-founder @ rmotr.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generators, generator expressions, and loops

2018-11-16 Thread Santiago Basulto
Try itertools.count()
.

On Fri, Nov 16, 2018 at 12:08 PM Steve Keller  wrote:

> Cancel ill-formated article
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 
Santiago Basulto.-
Co-founder @ rmotr.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generators, generator expressions, and loops

2018-11-16 Thread Matt Wheeler


> On 16 Nov 2018, at 14:54, Steve Keller  wrote:
> More elegant are generator expressions but I cannot think of a way
> without giving an upper limit:
> 
>for i in (2 ** i for i in range(100)):
>...
> 
> which looks ugly.  Also, the double for-loop (and also the two loops
> in the above exmaple, for + while in the generator) look unnatural,
> somehow, i.e. loop over all elements which are created by a loop.
> 
> Is there a more beautyful way?

from itertools import count

for i in (2**n for n in count()):


In general itertools includes a lot of useful stuff, it's worth reading the 
whole docs page: https://docs.python.org/3/library/itertools.html 



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


Re: IDLE Default Working Directory

2018-11-16 Thread Irv Kalb


> On Nov 15, 2018, at 2:44 PM, eryk sun  wrote:
> 
> On 11/14/18, Irv Kalb  wrote:
>> 
>> When working with data files, I tell students to put their project (their
>> main program and any other related files) in a folder.  Then, in their calls
>> to "open", I tell them to just give the name of the data file e.g.,
>> 'MyData.txt', or a path relative from the current folder, e.g.,
>> 'MyData/DataFile.txt'.  That makes things simple in a teaching environment
>> and works on both Macs and Windows.
> 
> I hope you provide code to change the working directory to the script
> directory (e.g. based on __file__, assuming it's not a frozen script).
> Don't let them assume that these are the same. A process can be
> started with any valid working directory. If you double click on a
> script in Explorer, it happens to set the working directory to the
> script directory. That's not necessarily the case for the Win+R run
> dialog, a shell command line, or generally any call that runs the
> script (e.g. system, spawnl, or WinAPI ShellExecuteEx and
> CreateProcess).
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

Actually, I do not provide code to change the working directory.  

As with the original poster, I teach students who *never* use the command line. 
 All work is done using IDLE, which simplifies their environment.   IDLE (and 
PyCharm) apparently set the working directory appropriately and relative paths 
always work properly.  

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


Python Interpreters In Eclipse

2018-11-16 Thread Jaxson Baerg
Hey, I'm attempting to use python in eclipse and am having trouble with the
python interpreter. I am using Eclipse 2018-09 and the latest PyDev and
Python 3.7.1. When declaring python-3.7.1.exe as an interpreter it gives an
error saying it was unable to get info on the interpreter. It also says a
possibility to why could be using an unsupported version. I don't know
where to go from here, another piece of information is that I am doing this
on a school computer with teacher permission but I don't know if that could
be causing an issue or not? Any help would be appreciated, thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Interpreters In Eclipse

2018-11-16 Thread Liste guru

Il 16/11/2018 21:08, Jaxson Baerg ha scritto:

Hey, I'm attempting to use python in eclipse and am having trouble with the
python interpreter. I am using Eclipse 2018-09 and the latest PyDev and
Python 3.7.1. When declaring python-3.7.1.exe as an interpreter it gives an
error saying it was unable to get info on the interpreter. It also says a
possibility to why could be using an unsupported version. I don't know
where to go from here, another piece of information is that I am doing this
on a school computer with teacher permission but I don't know if that could
be causing an issue or not? Any help would be appreciated, thanks!


    When setting a pydev interpreter for the standard Pyrthon you 
should point it to a file named python.exe (I suppose you are on 
windows) on an installed (or virtualenved) python, so probabably you 
just have to install it on windows , launching (outside eclipse) the 
python-3.7.1.exe and following the instruction.


    With best regards

    Daniele Forghieri


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


Re: Re: Generators, generator expressions, and loops

2018-11-16 Thread Peter via Python-list

Lovely, succinct answers.


On 17/11/2018 2:44 AM, Ian Kelly wrote:

On Fri, Nov 16, 2018 at 7:57 AM Steve Keller  wrote:

I have looked at generators, generator expressions, and iterators and
I try to get more familiar with these.

1. How would I loop over all (with no upper bound) integers or all
powers of two, for example?

In C it would be

for (int i = 0; ; i++) { ... }  or  for (int i = 1; ; i *= 2) { ... }

In Python, I could define a generator

 def powers():
 i = 1
 while True:
 yield(i)
 i *= 2

 for i in powers():
 ...

More elegant are generator expressions but I cannot think of a way
without giving an upper limit:

 for i in (2 ** i for i in range(100)):
 ...

which looks ugly.  Also, the double for-loop (and also the two loops
in the above exmaple, for + while in the generator) look unnatural,
somehow, i.e. loop over all elements which are created by a loop.

Is there a more beautyful way?

Some options:

from itertools import count

def powers():
 for i in count():
 yield 2 ** i


for i in (2 ** i for i in count()):
 ...


for i in map(lambda x: 2 ** x, count()):
 ...


from functools import partial
from operator import pow

for i in map(partial(pow, 2), count()):
 ...


Take your pick.




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