# Adding a simple way to create string prefixes

**URL:** <https://discuss.python.org/t/adding-a-simple-way-to-create-string-prefixes/60577>\
**Category:** Ideas\
**Created:** [August 11, 2024, 8:44pm UTC](https://discuss.python.org/t/adding-a-simple-way-to-create-string-prefixes/60577 "2024-08-11T20:44:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nietanod](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nietanod/32/21769_2.png) [@Nietanod](https://discuss.python.org/u/Nietanod)\
**Post date:** [August 11, 2024, 8:44pm UTC](https://discuss.python.org/t/adding-a-simple-way-to-create-string-prefixes/60577/1 "2024-08-11T20:44:56Z")

</div>

# Adding a simple way to create string prefixes.

My proposition is to add a way to make new strings prefixes more easily:

If you have a class like this:

```python
class MyClass:
    def __init__ (self, value):
        ...

```

> The ` __init__ ` method of the class must’ve only one positional argument.

You can add a decorator to the class:

```python
@prefix(“p”)
class MyClass:
    def __init__ (self, value):
        ...

```

So each time you write something like this:

```python
string = p”hello”

```

Python will interpret this line as:

```python
string = MyClass(“hello”)

```

This can improve the clarity of the code, making it more readable and more explicit when you need a repetitive type.

---

<div class="post-metadata">

**Author:** ![flyinghyrax](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/flyinghyrax/32/12394_2.png) [@flyinghyrax](https://discuss.python.org/u/flyinghyrax)\
**Post date:** [August 11, 2024, 8:51pm UTC](https://discuss.python.org/t/adding-a-simple-way-to-create-string-prefixes/60577/2 "2024-08-11T20:51:12Z")

</div>

[PEP 750: Tag Strings For Writing Domain-Specific Languages](https://discuss.python.org/t/pep-750-tag-strings-for-writing-domain-specific-languages/60408)

🙂

---

<div class="post-metadata">

**Author:** ![Nietanod](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nietanod/32/21769_2.png) [@Nietanod](https://discuss.python.org/u/Nietanod)\
**Post date:** [August 11, 2024, 9:01pm UTC](https://discuss.python.org/t/adding-a-simple-way-to-create-string-prefixes/60577/3 "2024-08-11T21:01:06Z")

</div>

My idea is more about the implementation, but thank you.

---

<div class="post-metadata">

**Author:** ![effigies](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/effigies/32/35835_2.png) [@effigies](https://discuss.python.org/u/effigies)\
**Post date:** [August 12, 2024, 1:02pm UTC](https://discuss.python.org/t/adding-a-simple-way-to-create-string-prefixes/60577/4 "2024-08-12T13:02:58Z")

</div>

With PEP750, you can write your own prefix decorator:

```python
def prefix(name):
    def decorator(func):
        globals()[name] = func
        return func
    return decorator

```

Trying it out on [their jupyterlite](https://pauleveritt.github.io/tagstr-site/playground/lab/index.html):

![Screenshot from 2024-08-12 09-01-50](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/6/1/613ceb745015a326f42758d480be89f8f79b95f0.png)

---

<div class="post-metadata">

**Author:** ![chepner](https://avatars.discourse-cdn.com/v4/letter/c/22d042/32.png) [@chepner](https://discuss.python.org/u/chepner)\
**Post date:** [August 15, 2024, 3:38pm UTC](https://discuss.python.org/t/adding-a-simple-way-to-create-string-prefixes/60577/5 "2024-08-15T15:38:49Z")

</div>

You are proposing the ability to change the _syntax_ of Python at runtime. Very few languages do this, as it makes parsing more complicated, and it’s especially incongruous for a language like Python which has traditionally strongly _constrained_ its grammar in the name of simplifying parsing.

(Prior to the PEG parser, the grammar was intentionally limited to an LL(1) grammar. I believe that’s one reason why `a if condition else b` was chosen over `if condition then a else b` for the conditional expression, because the latter would require additional–unlimited?–lookahead to distinguish from an ordinary `if` statement.)
