Yes, of course I understand that it is an experiment and 99% of your article is about adding your own syntax to Python which is super interesting and worth reading, but I believe it adds value to mention that it could be done already in pure Python.
Best regards!
Note: As you probably know in Python you can use False, 0, None, [], {} and '' as "not True" in "if" clauses, so in all cases: "if a:" should work later in your code, so the multiplication operator does work with str's:
Juan Francisco Roco Ah ok, now I see what you mean. I think the problem is that I chose I terrible example to illustrate this feature, as you showed.
Regarding this last example, the idea is to return None in case of False. Your example returns an empty string, which is not the same thing. Besides, the object must support __mul__ protocol, which is not the case for custom objects.
For example:
In [1]: classPerson:
...: def__init__(self, name):
...: self.name = name
...: def__bool__(self):
...: return bool(self.name)
...:
In [2]: p = Person('name')
In [3]: a = p * False
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-a2214d15ebd2> in <module>
----> 1 a = p * False
TypeError: unsupported operand type(s) for *: 'Person'and'bool'
In [4]: a = 'hello'if p
In [5]: a
Out[5]: 'hello'
In [7]: p = Person('')
In [9]: a = 'hello'if p
In [11]: type(a)
Out[11]: NoneType