# Enum for open() modes

**URL:** <https://discuss.python.org/t/enum-for-open-modes/2445>\
**Category:** Ideas\
**Created:** [October 8, 2019, 12:44pm UTC](https://discuss.python.org/t/enum-for-open-modes/2445 "2019-10-08T12:44:35Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![Marco\_Sulla](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/marco_sulla/32/1451_2.png) [@Marco\_Sulla](https://discuss.python.org/u/Marco_Sulla)\
**Post date:** [October 16, 2019, 9:25pm UTC](https://discuss.python.org/t/enum-for-open-modes/2445/7 "2019-10-16T21:25:26Z")

</div>

> I’m fairly sure you can do better than that just defining the appropriate dunders.

…Ok, it seems to me an overkill, but…

I can create the class `OpenFlag(str)`, with three attributes, `type`, `data` and `update`, that will implements ` __or__ ` in such a way that, for example, `a | b` will return a new `OpenFlag`, with the attributes of `a` and any of the three attributes of `b` that replaces the relative attribute of `a`, if it is not null and the relative attribute of `a` was null. If both are not null, an exception will be raised. The ` __repr__ ` of `OpenFlag` will return a string that is a concatenations of the three attributes, when they are not null. So the `enum` will became:

```python
class OpenMode(OpenFlag, Enum):
    read = OpenFlag(type="r")
    append = OpenFlag(type="a")
    truncate_and_write = OpenFlag(type="w")
    exclusive_create = OpenFlag(type="x")
    update = OpenFlag(update="+")
    binary = OpenFlag(data="b")

```

and, for example, `"x+b"` can be written as

`OpenMode.update | OpenMode.exclusive_create | OpenMode.binary`

Anyway no one can stop me to add also this one to the `enum` and name it `exclusive_create_and_update_binary`. You can use it or you can use the combination above or you can use `"x+b"`, it’s up to you. IMHO

`OpenMode.exclusive_create | OpenMode.binary | OpenMode.update`  
or  
`OpenMode.exclusive_create_and_update_binary`

are much more readable also to someone that doesn’t know `Python`.

It’s pretty because it’s more readable. It’s useful because is more descriptive (I always forget that `w` is **truncate** and write…). And it does replace nothing, it only adds a more readable way to explicit how you’re opening the file, that you can use or not.

And the point is _Readability counts_.

---

_[View the full topic](https://discuss.python.org/t/enum-for-open-modes/2445)._
