Ask Your Question

Revision history [back]

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():
    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

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

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():
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 will 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