ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 09 May 2020 09:49:22 -0500- Boolean answerhttps://ask.sagemath.org/question/51349/boolean-answer/ `bool()` has answer `True` or `False`. Could it be translated. For my case in French `vrai` et `faux` ?Sat, 09 May 2020 09:04:27 -0500https://ask.sagemath.org/question/51349/boolean-answer/
- Answer by slelievre for <p><code>bool()</code> has answer <code>True</code> or <code>False</code>. Could it be translated. For my case in French <code>vrai</code> et <code>faux</code> ?</p>
https://ask.sagemath.org/question/51349/boolean-answer/?answer=51352#post-id-51352The 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 crazySat, 09 May 2020 09:49:22 -0500https://ask.sagemath.org/question/51349/boolean-answer/?answer=51352#post-id-51352