0

I'm wondering if there is anyway around the typical limitations on python function names (alphanumerics etc).

Typical function application proceeds like this myFunction(arg1,arg2). But we know there are exceptions, such as functions that use infix notation. As example, consider 3 + 5. Here, the addition function uses infix notation, and also has a rather special name +.

My question is if there is any way (perhaps with aliases) to give a function a name that is a special character such as + or ~. This possibility obviously exists in core python, but is there a way utilize this with defined functions? Thanks

keegan
  • 2,892
  • 1
  • 17
  • 20
  • 2
    `+` is syntax which maps to the [`object.__add__`](https://docs.python.org/2/reference/datamodel.html#object.__add__) method. You can't create new syntax, you can only create new objects. – Peter Wood Mar 23 '15 at 20:55
  • What exactly are you trying to achieve? You can extend the parser if you really want, but it's unlikely to be worth the effort. – jonrsharpe Mar 23 '15 at 20:59
  • Just trying to understand the special case `+` and wondering if I could utilize something similar. – keegan Mar 23 '15 at 21:00

1 Answers1

1

My question is if there is any way (perhaps with aliases) to give a function a name that is a special character such as + or ~.

No. For a handful of cases you might be able to do something similar though:

Python doesn't have the concept of infix functions, like e.g. Haskell does.

The operators that it does have (+, -, *) are hardcoded in the sense that you can't add new ones, however, you can override what each of these operators do when applied to a particular type. This is called operator overloading and is described in another answer.

Markus Unterwaditzer
  • 7,992
  • 32
  • 60