Ask Your Question
0

Boolean answer

asked 2020-05-09 16:04:27 +0100

Cyrille gravatar image

bool() has answer True or False. Could it be translated. For my case in French vrai et faux ?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2020-05-09 16:49:22 +0100

slelievre gravatar image

updated 2020-05-10 12:42:33 +0100

The booleans True and False are Python built-ins. I doubt there is a way to change how they display.

A workaround is to build a class Bool to mimic bool in French:

class Bool(int):
    r"""
    Booléens francisés.

    EXAMPLES:

        sage: v = Bool(1)
        sage: v
        Vrai
        sage: bool(v)
        True

        sage: f = Bool(0)
        sage: f
        Faux
        sage: bool(f)
        False
    """

    def __init__(self, b):
        self.bool = bool(b)

    def __repr__(self):
        return "Vrai" if self.bool else "Faux"

    def __str__(self):
        return "Vrai" if self.bool else "Faux"

    def __bool__(self):
        return self.bool

Note that we make Bool a subclass of int to better mimic Python's bool class (which itself cannot be subclassed).

Use Bool instead of bool in your code and the results should behave much the same as bool would, except they display as Vrai and Faux instead of True and False.

To go further, you might even decide to call this class bool instead of Bool, thus overriding the builtin class bool, and then you would not need to change your other code.

I wonder if that might have unexpected side-effects.

To be more cautious, call the class Bool and then run

bool = Bool

Revert to the usual booleans if needed by running

from builtins import bool

or

import builtins
bool = builtins.bool

If you really like having booleans display in French and don't mind the unusual setup, you could put the definition of Bool and the bool = Bool in the file ~/.sage/init.sage so it is run each time Sage starts (first create that file if it does not already exist).

Other ideas of how to name this class:

  • FrenchBool for a more descriptive name
  • fool for these French booleans that some might find a little crazy
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-05-09 16:04:27 +0100

Seen: 242 times

Last updated: May 10 '20