开发者

Is there a way to create a function that does not need parentheses?

开发者 https://www.devze.com 2023-03-31 13:51 出处:网络
Well, probab开发者_C百科ly a strange question, I know. But searching google for python and braces gives only one type of answers.

Well, probab开发者_C百科ly a strange question, I know. But searching google for python and braces gives only one type of answers.

What I want to as is something low-level and, probably, not very pythonic. Is there a clear way to write a function working with:

>>>my_function arg1, arg2

instead of

>>>my_function(arg1, arg2)

?

I search a way to make function work like an old print (in Python <3.0), where you don't need to use parentheses. If that's not so simple, is there a way to see the code for "print"?


You can do that sort of thing in Ruby, but you can't in Python. Python values clean language and explicit and obvious structure.

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!


The requirement for braces lies in the Python interpreter and not in the code for the print method (or any other method) itself.

(And as eph points out in the comments, print is a statement not a method.)


As you've already been told, the print in Python 2.x was not a function, but a statement, like if or for. It was a "1st class" citizen, with its own special syntax. You are not allowed to create any statement, and all functions must use parentheses (both in Python 2.x and in 3.x).


No. Function call needs parentheses and two directly consecutive identifiers (that excludes reserved words) are a syntax error. That's set in stone in the grammar and won't change. The only way you could support this is making your own language implementation, at least the frontend of one - that's likely more trouble than it's worth, and would require some significant learning on your side (unless of course you aren't already a parsing expert). See numerous stack overflow questions on compiler construction for material if you want to try it nevertheless.


What are you trying to do? If you are trying to embed this code into another program (non-python) or invoke from the interpreter somehow, can you use sys.argv as an alternative instead? Here is an example of how sys.argv works.

0

精彩评论

暂无评论...
验证码 换一张
取 消