Posts: 4,647
Threads: 1,494
Joined: Sep 2016
... coding tips like this?
if you have ever coded something like this: if n==1:
s = ''
else:
s = 's'
...
print('we found',n,'thing'+s) or the shorter way many people hate: s = '' if n==1 else 's'
...
print('we found',n,'thing'+s) then you might want to consider doing it like this: s = ('s','')[n==1]
...
print('we found',n,'thing'+s) this works because booleans False and True can be used anywhere a number can be used: Output: >>> complex(False)
0j
>>> complex(True)
(1+0j)
>>> True/2
0.5
>>>
note that the last one will give a different result in Python2.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,220
Threads: 97
Joined: Sep 2016
(Sep-19-2018, 03:00 AM)Skaperen Wrote: this works because booleans False and True can be used anywhere a number can be used:
I don't think this statement is quite correct. It suggests that booleans are not numbers. But they are.
Python Docs Wrote:There are three distinct numeric types: integers, floating point numbers, and complex numbers. In addition, Booleans are a subtype of integers.
Posts: 591
Threads: 26
Joined: Sep 2016
We have ternary statements for a reason.
Abusing indexing to accomplish the same thing, while clever, is objectively terrible.
Posts: 2,342
Threads: 62
Joined: Sep 2016
That hack is why the ternary operator was introduced, I believe. The hack doesn't short-circuit like the ternary operator does, by the way, which can occasionally lead to surprise-slow code.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
(Sep-19-2018, 03:09 AM)ichabod801 Wrote: (Sep-19-2018, 03:00 AM)Skaperen Wrote: this works because booleans False and True can be used anywhere a number can be used:
I don't think this statement is quite correct. It suggests that booleans are not numbers. But they are.
Python Docs Wrote:There are three distinct numeric types: integers, floating point numbers, and complex numbers. In addition, Booleans are a subtype of integers.
i see them as an abstraction. they do have some distinct methods.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 2,342
Threads: 62
Joined: Sep 2016
(Sep-19-2018, 07:29 PM)Skaperen Wrote: they do have some distinct methods. Python 3:
Output: >>> set(dir(bool)) - set(dir(int))
set()
>>> set(dir(int)) - set(dir(bool))
set()
>>> len(dir(int)), len(dir(bool))
(70, 70)
>>> set(dir(int)) == set(dir(bool))
True
???
Posts: 4,220
Threads: 97
Joined: Sep 2016
(Sep-19-2018, 07:29 PM)Skaperen Wrote: i see them as an abstraction.
You can see them however you want. That doesn't change what they are.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
(Sep-20-2018, 01:42 AM)ichabod801 Wrote: (Sep-19-2018, 07:29 PM)Skaperen Wrote: i see them as an abstraction.
You can see them however you want. That doesn't change what they are. where is what they are defined, as opposed to the implementation? typically, booleans are implemented as numbers. but that does not make them be numbers. maybe the really correct statement is that Python treats them as numbers universally and implements bools as numbers in most cases ( str() doesn't, for example).
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,220
Threads: 97
Joined: Sep 2016
You were talking about booleans in Python. Booleans in Python are numbers. They are a subclass of int. That makes them a number.
And for the general case, given boolean algrebra, I would be curious as to how you are distinguishing booleans from numbers.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
Sep-21-2018, 08:03 PM
(This post was last modified: Sep-21-2018, 08:07 PM by Skaperen.)
(Sep-19-2018, 07:37 PM)micseydel Wrote: (Sep-19-2018, 07:29 PM)Skaperen Wrote: they do have some distinct methods. Python 3:
Output: >>> set(dir(bool)) - set(dir(int))
set()
>>> set(dir(int)) - set(dir(bool))
set()
>>> len(dir(int)), len(dir(bool))
(70, 70)
>>> set(dir(int)) == set(dir(bool))
True
??? just because it has methods with the same names does not mean they act the same.
it does look like python type bool is implemented as int in range(2) with special strings for the __str__ method. and the point is? many languages implement booleans that way. implementations don't change what booleans are.
and it's not the same as modulo 2.
do programmers need to know how python implements its types to be able to code them in python? maybe it's the other way around: seeing how their code behaves suggests how the implementation was done.
somewhile back (i'm not going to look for it to see who) someone said they disliked seeing ternarys being used. i had tried avoiding them. so i should go back to using them?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
|