0

In Python, the builtin classes have alias-like things.

The str class can be used as: print(str("hi")), but there is an alias so you can just use: print("hi").

There is also an alias for dict: You can use dict() or {}.

I have my own class, myClass()

I can call it using myClass(a=1,b=2), but I want to be able to use it like: - a=1, b=2 - or

-
a=1,
b=2
-

The character doesn't need to be -, I just chose it as an example.

My class can accept any number of arguments, so if it is possible I want to also be able to make an empty myClass like this: --

and the ability for it to take as many args as needed:

-
a=1,
b=2,
c=3,
d=4,
e=5,
...
-
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 6
    No, Python doesn't allow you to invent your own syntax like that. – khelwood Dec 26 '19 at 19:10
  • 4
    Your "alias" is a [*literal*](https://docs.python.org/3/reference/lexical_analysis.html#literals), and you can't define your own without modifying the interpreter itself. Also note that your use of `str` *also* involves a string literal. – jonrsharpe Dec 26 '19 at 19:13
  • Sorta related: [Can you add new statements to Python's syntax?](https://stackoverflow.com/q/214881/4518341) – wjandrea Dec 26 '19 at 19:42
  • Does this answer your question? [Making your own statements](https://stackoverflow.com/questions/2222843/making-your-own-statements) - it's about statements (keywords), but 90% of the same rules apply to special character tokens. – wjandrea Dec 26 '19 at 19:50
  • 3
    The "alias" for `dict` is not an alias. Try `dict({'x': 'y'})` vs `{{'x': 'y'}}`. – Matthias Dec 26 '19 at 20:19
  • 1
    These things *are not aliases at all*. In some cases, you are talking about literals, which **are not equivalent to the type constructors at all* and certainly not aliases. Also, `print(str(x))` is not an alias for `print(x)`. And `dict` is not an alias for `dict()`, this is just fundamentally confused. One merely is a reference to the class object, the other is calling the class constructor – juanpa.arrivillaga Dec 26 '19 at 21:41

0 Answers0