# Uniform Function Call Syntax (UFCS)

**URL:** <https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774>\
**Category:** Ideas\
**Created:** [August 13, 2024, 7:59pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774 "2024-08-13T19:59:12Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![Dutcho](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dutcho/32/7074_2.png) [@Dutcho](https://discuss.python.org/u/Dutcho)\
**Post date:** [August 14, 2024, 2:18pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/21 "2024-08-14T14:18:57Z")

</div>

E.g. [`duckdb`](https://duckdb.org/docs/api/python/relational_api) has tables (relations to be precise) with methods that return a table, to which one can apply a method, which returns a table, to which one can apply a method, which returns a table, etc.

```python
# Total salary of IT in Aug (I think, on mobile, no laptop)
(duckdb.table('employees')
       .filter("department = 'IT'")
       .join(duckdb.table('emp_salaries')
             .filter("year_month = 202408"), 
             'emp_id')
       .aggregate('sum(salary)')
       .show()
)

```

With extra brackets this works (because they are methods).

Now suppose I have a function, e.g. `natural_join()` that avoids specification of the join column:

```python
def natural_join(t1, t2):
    common = ', '.join(set(t1.columns) & set(t2.columns))
    return t1.join(t2, common)

```

Then I cannot use the same style, but instead need to:

```python
(natural_join(duckdb.table('employees')
                    .filter("department = 'IT'"),
              duckdb.table('emp_salaries')
                    .filter("year_month = 202408"))
              .aggregate('sum(salary)')
              .show()
)

```

My understanding of the proposed syntax is that it allows something closer to the original:

```python
(duckdb.table('employees')
       .filter("department = 'IT'")
       .natural_join(duckdb.table('emp_salaries')
                           .filter("year_month = 202408"))
       .aggregate('sum(salary)')
       .show()
)

```

While that’s attractive for _this_ use case, `object().print()` is _not_, so I’d like to know how to prevent such use.

---

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [August 14, 2024, 3:11pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/22 "2024-08-14T15:11:38Z")

</div>

> [@Dutcho](#):
>
> suppose I have a function, e.g. `natural_join()`

Subclassing would help, but if the DuckDB API doesn’t allow subclassing, that’s not Python’s problem. 🙂

> [@Dutcho](#):
>
> object().print()

That’s exactly what I would avoid when writing code. It may look cool and easy as a one-liner, but the next morning, you won’t understand what you wrote.

You either need to add necessary comments or, preferably, use descriptive variables for each function call.

---

<div class="post-metadata">

**Author:** ![Dutcho](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dutcho/32/7074_2.png) [@Dutcho](https://discuss.python.org/u/Dutcho)\
**Post date:** [August 14, 2024, 3:30pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/23 "2024-08-14T15:30:37Z")

</div>

> [@elis.byberi](#):
>
> Subclassing would help, but if the DuckDB API doesn’t allow subclassing, that’s not Python’s problem. 🙂

I don’t think I can subclass, because (other than via class methods) I cannot create a relation instance, so no ` __new__ ()` to `super()` to. I’ll try. Anyhow subclassing seems a heavy tool for such utility additions (potentially resulting in multiple inheritance). So the problem is mine, then 😅

And yes, `object().print()` should definitely _not_ be possible. SQL is often written in a piped mode (also see e.g. [PRQL](https://prql-lang.org/)), imperative code shouldn’t.

I don’t know whether I share your preference for descriptive variables for _each_ (emphasis mine) function call. Linux wouldn’t be the same with all the `>` and `< temp_file`s.

---

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [August 14, 2024, 3:58pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/24 "2024-08-14T15:58:18Z")

</div>

> [@ajoino](#):
>
> OoT, but what do you mean with “it cannot be overridden”?

No, he was referring to a different one, namely, `dict( **A,** B)`. This one raises an error on duplicate keys.

`{ **A,** B}` can overwrite, but does not respect types and converts to pure `dict`.

---

<div class="post-metadata">

**Author:** ![frequenti](https://avatars.discourse-cdn.com/v4/letter/f/71c47a/32.png) [@frequenti](https://discuss.python.org/u/frequenti)\
**Post date:** [August 15, 2024, 3:51pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/25 "2024-08-15T15:51:47Z")

</div>

why it is not clear enough?

Intermediate variable cost more mentally. It more clear to run though all process in one go, like recieps.

---

<div class="post-metadata">

**Author:** ![NeilGirdhar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/neilgirdhar/32/5776_2.png) [@NeilGirdhar](https://discuss.python.org/u/NeilGirdhar)\
**Post date:** [August 15, 2024, 5:10pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/26 "2024-08-15T17:10:54Z")

</div>

I didn’t say it wasn’t “clear enough”. I said:

> [@NeilGirdhar](#):
>
> It would be different if a pattern [were] significantly clearer or simpler, mitigates errors, or [were] more expressive.

For me, I think this proposal would be a lot stronger with real life examples showing how this pattern has significant benefits over idiomatic Python. I’m not convinced that “functional programming is better than imperative programming”. As in life, everyone has different experience, and if you want to be convincing, it helps to relate your experience rather than your conclusions.

* * *

Also, I disagree that intermediate variables are always worse. Their names function as comments, and they facilitate debugging. Maybe there are cases where they’re superfluous. However, any new functional syntax is also noisy when you’re used to reading imperative code.

---

<div class="post-metadata">

**Author:** ![Dutcho](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dutcho/32/7074_2.png) [@Dutcho](https://discuss.python.org/u/Dutcho)\
**Post date:** [August 15, 2024, 8:31pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/27 "2024-08-15T20:31:21Z")

</div>

> [@NeilGirdhar](#):
>
> real life examples

E.g. compare earlier duckdb fragment:

```python
(duckdb.table('employees')
       .filter("department = 'IT'")
       .natural_join(duckdb.table('emp_salaries')
                           .filter("year_month = 202408"))
       .aggregate('sum(salary)')
       .show()
)

```

with ‘idiomatic’ form with temporary values:

```python
employees = duckdb.table('employees')
it_employees = employees.filter("department = 'IT'")
emp_salaries = duckdb.table('emp_salaries')
aug_salaries = emp_salaries.filter("year_month = 202408")
aug_salaries_of_it_employees = natural_join(it_employees, aug_salaries)
total_aug_salaries_of_it_employees = aug_salaries_of_it_employees.aggregate('sum(salary)')
total_aug_salaries_of_it_employees.show()

```

---

<div class="post-metadata">

**Author:** ![NeilGirdhar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/neilgirdhar/32/5776_2.png) [@NeilGirdhar](https://discuss.python.org/u/NeilGirdhar)\
**Post date:** [August 15, 2024, 8:38pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/28 "2024-08-15T20:38:03Z")

</div>

Good example. I guess it’s a matter of taste, but with the variable names, I actually find it easier to understand what’s going on in the imperative case.

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [August 15, 2024, 9:08pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/29 "2024-08-15T21:08:25Z")

</div>

Also, the imperative form is not written particularly clearly. Better would be

```python
emp_data = duckdb.table('employees').filter("department = 'IT'")
sal_data = duckdb.table('emp_salaries').filter("year_month = 202408")
agg_salaries = natural_join(emp_data, sal_data).aggregate('sum(salary)')
agg_salaries.show()

```

That reads very clearly to me. It’s also easier to debug, as you can inspect the two parts of the natural join without having to rewrite the code. I also used shorter variable names - that’s personal preference, but IMO the extremely long names @Dutcho used obscure the meaning rather than clarifying it.

It’s all very much a matter of taste, but the advantage of the imperative form is that it doesn’t need a change to the Python language 🙂

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [August 15, 2024, 9:34pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/30 "2024-08-15T21:34:31Z")

</div>

> [@NeilGirdhar](#):
>
> I guess it’s a matter of taste, but with the variable names, I actually find it easier to understand what’s going on in the imperative case.

It’s definitely a matter of taste. My personal taste is that the variable names don’t help, but that that’s not the fault of the variable names… the problem is that this is SQL masquerading as Python, and it would be FAR clearer written like this:

```python
duckdb.query("""select sum(salary)
    from employees natural join emp_salaries
    where department = 'IT'
    and year_month = 202408
""")

```

Especially since (as evidenced by the `filter` clauses) you still need to be aware of the foibles of SQL, so you gain very little from doing it as a bunch of methods.

I’ve seen ORMs done well, but this isn’t a good example, and I don’t think it’s a good showcase in the debate on pipelining vs imperative. The intermediate variables are unhelpful since only the resultant query is really meaningful here; but on the other hand, the `.show()` at the end is pure noise that exists for the sake of permitting incomplete queries to be unevaluated objects. (Unless that’s actually a “display to console” method, in which case it’s not PURE noise, but still, you could replace `query(...)` with `show(...)` and it’d be just as easy.)

---

<div class="post-metadata">

**Author:** ![petercordia](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@petercordia](https://discuss.python.org/u/petercordia)\
**Post date:** [September 12, 2024, 12:40pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/31 "2024-09-12T12:40:35Z")

</div>

@bwoodsend :

1. I think I mentioned this in the OP, that I agree, and that’s the reason I was thinking about what alternative symbol would be good. I still don’t have a good answer, and I was using ▼ thus far. I’ll use `|>` for this post to see how it feels.
2. Fair point. These are solvable problems, but it means implementing pseudo-UFCS would require yet more work.
3. 🤔 . I hate being surprised by side-effects too. I hadn’t thought about the fact that this option would encourage people to hide their side effects.

@BrenBarn :  
I would propose that `|>` acts as a very strongly binding `partial`, so that `func = obj|>print` results in `func==partial(obj, print)`.  
I think this avoids the errors. Then `func==partial(obj, print); func()` is fine.

# use cases:

At the moment the main situation where I’d like to have pseudo-UFCS is in situations where I write

```python
arg = func(obj.method(*args))

```

which feels ugly to me.

I’d prefer to be able to write

```python
arg = obj.method(*args)|>func()

```

Recent examples are situations where I want to convert a Path.glob() iterator into a list, or to know it’s length, or to know whether it has any elements, or to get it’s first element. I get to the end of my line of code, then suddenly I have to stop, go back to the start, type the function, add brackets around the whole thing. It’s disruptive. The flow of programming suddenly goes backwards.  
And I know readability is more important than ease of writing, but personally I find

```python
file = Path(SOURCE_ROOT_PATH).glob("*/proprietory/pattern/probably/maybe*"). __next__ ()

```

easier to read than

```python
file = next(Path(SOURCE_ROOT_PATH).glob("*/proprietory/pattern/probably/maybe*"))

```

Then again, I also found a lot of examples in my code where writing `obj.method(*args)|>func()` would make the code worse. Most of the examples my regex grep turned up are best left as-is to be honest.

The other use-case is when you’re working with a library that has good support for method chaining. Working with dataframes for example. It’s annoying when custom-written functions disrupt the esthetic of the library. I mean, I like the way this looks:

```python
result = (
    df[df["A"] > 2]
    .groupby("B")
    .agg({"C": "mean"})
    .rename(columns={"C": "Mean"})
    .sort_values(by="Mean", ascending=False)
    .reset_index()
)

```

every line does exactly 1 thing, I don’t need much working memory to comprehend it, I don’t need to check that the names match, etc.  
And if I want to debug something like this, it would help me to be able to write

```python
result = (
    df[df["A"] > 2]
    .groupby("B")
    .agg({"C": "mean"})
    |>my_log()
    .rename(columns={"C": "Mean"})
    .sort_values(by="Mean", ascending=False)
    .reset_index()
)

```

for example instead of my current technique

```python
result[0] = (
    df[df["A"] > 2]
    .groupby("B")
    .agg({"C": "mean"})
)
result[1] = (
    result[0]
    .rename(columns={"C": "Mean"})
    .sort_values(by="Mean", ascending=False)
    .reset_index()
)

```

---

<div class="post-metadata">

**Author:** ![NeilGirdhar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/neilgirdhar/32/5776_2.png) [@NeilGirdhar](https://discuss.python.org/u/NeilGirdhar)\
**Post date:** [September 12, 2024, 3:30pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/32 "2024-09-12T15:30:35Z")

</div>

> [@petercordia](#):
>
> And I know readability is more important than ease of writing, but personally I find
> 
> ```python
> file = Path(SOURCE_ROOT_PATH).glob("*/proprietory/pattern/probably/maybe*"). __next__ ()
> 
> ```
> 
> easier to read than
> 
> ```python
> file = next(Path(SOURCE_ROOT_PATH).glob("*/proprietory/pattern/probably/maybe*"))
> 
> ```

I think it would be easier if you just broke up your lines:

```python
source_root_path = Path(SOURCE_ROOT_PATH)
files = source_root_path.glob("*/proprietory/pattern/probably/maybe*")
file = next(files)

```

> [@petercordia](#):
>
> And if I want to debug something like this, it would help me to be able to write

If you really want to, you could work around this by deriving from the dataframe class:

```python
class LoggableDataFrame(DataFrame):
  def log(self) -> Self: ...

df[LoggabaleDataFrame(df["A"]) > 2].groupby...........log()........

```

That should work as long as pandas hasn’t done anything too strange.

---

<div class="post-metadata">

**Author:** ![saaketp](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/saaketp/32/6703_2.png) [@saaketp](https://discuss.python.org/u/saaketp)\
**Post date:** [September 12, 2024, 5:00pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/33 "2024-09-12T17:00:18Z")

</div>

> [@petercordia](#):
>
> The other use-case is when you’re working with a library that has good support for method chaining. Working with dataframes for example. It’s annoying when custom-written functions disrupt the esthetic of the library.

pandas provides a method for this btw  
[https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pipe.html](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pipe.html)

Overall I like the idea of general pipes, they are very nice to use in the functional programming languages that have them. But I am not sure if I like the proposed syntaxes here.

You can use pipes already with third party libraries that do operator overloading for `|` and some extra magic, like this one:

> **[sspipe](https://pypi.org/project/sspipe/)**
>
> Simple Smart Pipe Operator

---

<div class="post-metadata">

**Author:** ![petercordia](https://avatars.discourse-cdn.com/v4/letter/p/bb73d2/32.png) [@petercordia](https://discuss.python.org/u/petercordia)\
**Post date:** [September 13, 2024, 12:11pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/34 "2024-09-13T12:11:59Z")

</div>

@NeilGirdhar :  
Yes, I probably should use `source_root_path = Path(SOURCE_ROOT_PATH)`.  
However, `files=...glob(...); file = next(files)` is absolutely awful for me. For the rest of the time that I’d be reading that function, I’d have the information in my head “files is the result of the glob pattern XXX, from which I consumed the first item.” That’s nasty complex information, that takes a chunk out of my working memory, completely uselessly.

As for subclassing from DataFrame, your example actually serves to illustrate (one of the reasons) why I despice subclassing. I don’t know whether there is a word for this. It’s not a footgun. But it’s a construction that’ll lie in wait for you and introduce nasty hard-to-trace bugs. Maybe one of the DataFrame methods constucts an actual DataFrame in its implementation where you expected it to modify the input, so suddenly unknowingly you have converted your LoggableDataFrame into a DataFrame, and an entirely different place in your program raises an AttributeError. The error and the Error could be in entirely separate files that don’t even talk with each other, but are both imported into a common function.

@saaketp :  
Thanks for pointing out pandas…pipe. That’ll be useful when I get back to working with Dataframes.

I don’t see myself working with sspipe, because the syntax doesn’t look comfortable enough.

And I agree the proposed syntax isn’t nice. `|>` is just 2 of the common pipe operators stuck together. It doesn’t read well. That’s part of why this is an ‘idea’ and not a ‘proposal’ actually. I couldn’t come up with a good symbol, and I wanted to open discussion to see whether other people could come up with something nice.  
Syntax comfort/esthetics is important enough for me to reject sspipe, and it’s probably important enough to reject `|>`.

Thinking of it as a ‘pipe method’ would nudge me in the direction of `.|`  
What would you think of `"Hello World".|print()` ? 😉

---

<div class="post-metadata">

**Author:** ![NeilGirdhar](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/neilgirdhar/32/5776_2.png) [@NeilGirdhar](https://discuss.python.org/u/NeilGirdhar)\
**Post date:** [September 13, 2024, 6:40pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/35 "2024-09-13T18:40:55Z")

</div>

> [@petercordia](#):
>
> As for subclassing from DataFrame, your example actually serves to illustrate (one of the reasons) why I despice subclassing.

It’s not really an issue with subclassing thought. The problem is that the pattern (`x.f().g().h()`) is not common in Python, and so there isn’t a good way to extend the methods you can call on it. Ideally, the library should ensure that any dataframes returned by a method in the chain have type `Self`. If they can’t promise that, then you can’t use inheritance to inject methods.

Honestly though I think that the problem illustrated in this idea might need to be explored. It does happen fairly often that you have a sequence of methods, each of which each return a modified copy of some input object. Consider:

```python
def f(state):
  y, state = g(state, x)
  state = h(state)
  i(state) # Forgot to accept modified state and overwrite state!
  statex = j(state) # Accidentally wrote to statex! (Linter may catch unused variable.)
  return state

```

This mirrors the example in this post, but illustrates two actual potential bugs that might be caught with some clever syntax may be able to detect and prevent these problems.

One idea would be to add a syntax like:

```python
def f(some_state):
  inject some_state as state:
    y = g(x) # state is removed from the returned tuple assuming that the tuple is a namedtuple
    h()
    i()
    j()
  return state

```

This probably still needs some way to mark which lines need this injection of state. Still, I can’t see how this would be worth it, but I think it addresses the original problem, maintains Python’s imperative style, and doesn’t introduce any weird punctuation.

---

<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:** [September 13, 2024, 10:36pm UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/36 "2024-09-13T22:36:17Z")

</div>

It’s quite funny to me that some folks think `|>` doesn’t look nice as a pipe operator - thats the actual forward “pipe” operator (reverse application) in the OCaml family of languages 😆

([OCaml library : Stdlib](https://ocaml.org/manual/5.2/api/Stdlib.html#VAL(%7C%253E)))

---

<div class="post-metadata">

**Author:** ![avi.gross](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@avi.gross](https://discuss.python.org/u/avi.gross)\
**Post date:** [September 14, 2024, 2:31am UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/37 "2024-09-14T02:31:55Z")

</div>

> [@flyinghyrax](#):
>
> It’s quite funny to me that some folks think `|>` doesn’t look nice as a pipe operator - thats the actual forward “pipe” operator (reverse application) in the OCaml family of languages 😆

Just FYI, R added a pipe symbol and also chose `|>` as the symbol. People had been using pipes for years using add-on packages and `%>%` was not necessarily as easy on the eyes, not to mention some variants perhaps rarely used like `%<>%`, `%$%`, `%!>%`, or `%T>%`.

Combinations of existing ASCII symbols that do not cause conflicts are not trivial to find. And, unlike R, python may not easily support arbitrary user-created operators like the percent sign bracketed ones R allows or things in grave accents that allow quite a bit more.

Of course, borrowing from another language implementation is not uncommon.

---

<div class="post-metadata">

**Author:** ![saaketp](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/saaketp/32/6703_2.png) [@saaketp](https://discuss.python.org/u/saaketp)\
**Post date:** [September 14, 2024, 11:36am UTC](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774/38 "2024-09-14T11:36:03Z")

</div>

> [@petercordia](#):
>
> I agree the proposed syntax isn’t nice. `|>` is just 2 of the common pipe operators stuck together

Actually that was the part I don’t have problem with, since I have used same syntax in [F# pipelines](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/functions/#pipelines).  
What I find weird specifically in your example is the stuff with parenthesis, where one argument is coming from the pipeline operator and rest provided inside `()`.

[Previous page](https://discuss.python.org/t/uniform-function-call-syntax-ufcs/60774.md?page=1)
