Python Forum
is it ok to post ...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is it ok to post ...
#1
... 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.
Reply
#2
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
We have ternary statements for a reason.
Abusing indexing to accomplish the same thing, while clever, is objectively terrible.
Reply
#4
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.
Reply
#5
(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.
Reply
#6
(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
???
Reply
#7
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(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.
Reply
#9
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020