---
title: "Discussion on \"How I Patched Python to Include This Ruby Feature\" | Hashnode"
description: "Discussion on \"How I Patched Python to Include This Ruby Feature\". How I modified python’s source code and compiled it to accept “else-less” if expressions"
url: "https://hashnode.com/posts/what-if-python-had-this-ruby-feature/61178ef6e9f08a51fd85f74c"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How I Patched Python to Include This Ruby Feature](https://miguendes.me/what-if-python-had-this-ruby-feature?utm_source=hashnode&utm_medium=feed)

How I modified python’s source code and compiled it to accept “else-less” if expressions

Aug 14, 2021 · 18 min read · miguendes.me  
Updated Feb 20, 2022  
Read the article

-   [#python](https://hashnode.com/tag/python)
-   [#python-projects](https://hashnode.com/tag/python-projects)

## Written by

[Miguel Brito](https://hashnode.com/@miguendes)

@miguendes · London, UK

  
[View profile](https://hashnode.com/@miguendes)

Machine Learning Software Engineer - Hobbyst Technical Writer - Interested in Software Testing, Best Practices, Scalability, and Python.

AI Software Engineer based in London, UK with 6+ years of professional experience developing and releasing software in different programming languages - Hobbyist Technical Writer - Interested in Software Testing, Best Practices, Scalability, Machine Learning/AI, and Python.

## Responses 2

Markdown supported · ⌘↵ to post

[Juan Francisco Roco](https://hashnode.com/@jfroco)

[Sep 23, 2021](https://hashnode.com/posts/what-if-python-had-this-ruby-feature/61178ef6e9f08a51fd85f74c/comment/614c8b04544e242e0c93eec1)

Congratulations... I read about this on PyCoder's Weekly

Isn't this enough for your use case (pure and simple Python)?

```

>>> 42 * True
42
>>> 21 * False
0

>>> a = 42
>>> a += 1 * False
>>> a
42

>>> a += 1 * True
>>> a
43
```

[Miguel Brito](https://hashnode.com/@miguendes)

[Sep 23, 2021](https://hashnode.com/posts/what-if-python-had-this-ruby-feature/61178ef6e9f08a51fd85f74c/comment/614c8b04544e242e0c93eec1#reply-614cb369544e242e0c93f43b)

Hey Juan, I didn't actually submit this as PR to python. The language remains the same.

This post is just an fun experiment.

The code you suggest has the same result indeed but this example is just an illustration.

There are other use cases as well where this trick wouldn't work.

For example

```
>>> a = 'hello' * False
>>> type(a)
str
>>> a = 'hello' if False # assuming it uses the patch I did
>>> type(a) 
NoneType
```

As you can see, your suggestion only work for integers, the "else-less" solves other problems as well.

I hope that answers your question and thanks for your comment!

[Juan Francisco Roco](https://hashnode.com/@jfroco)

[Sep 23, 2021](https://hashnode.com/posts/what-if-python-had-this-ruby-feature/61178ef6e9f08a51fd85f74c/comment/614c8b04544e242e0c93eec1#reply-614cbed5544e242e0c93f5bf)

Hello [Miguel Brito](https://hashnode.com/@miguendes)

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:

```
>>> a = 'hello' * True
>>> a
'hello'
>>> a = 'hello' * False
>>> a
''
>>> if a:
...     print "True"
...
>>>
```

[Miguel Brito](https://hashnode.com/@miguendes)

[Sep 25, 2021](https://hashnode.com/posts/what-if-python-had-this-ruby-feature/61178ef6e9f08a51fd85f74c/comment/614c8b04544e242e0c93eec1#reply-614f3f377ef2d57e218ef61d)

[Juan Francisco Roco](https://hashnode.com/@jfroco) 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]: class Person:
   ...:     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
```

[Paula Maranhão](https://hashnode.com/@pmaranhaor)

[Aug 16, 2021](https://hashnode.com/posts/what-if-python-had-this-ruby-feature/61178ef6e9f08a51fd85f74c/comment/611a7360888ca15f5d38ea48)

Wow! So… can I make Python behave like another language other than Ruby?
