In R
, I can create a collapsible block of codes using
在R中,我可以使用以下命令创建可折叠的代码块
{
## My R code
}
I am wondering if there is any similar approach for Python
? Clearly above {}
is not working in Python
. I tried with :
, but still get error.
我想知道是否有类似的方法来处理Python?显然,上面的{}在Python中不起作用。我尝试使用:,但仍然出错。
Any pointer will be very helpful.
任何指针都会非常有用。
更多回答
优秀答案推荐
No, there is no similar approach in Python. You can make use of methods or classes, which are close to collapsible block of codes, eg.:
不,在Python中没有类似的方法。您可以使用接近可折叠代码块的方法或类,例如:
def foo():
# do something
if __name__ == "__main__":
foo()
Nevertheless there is no possibility to create such code blocks in a simple script without using methods or classes.
然而,如果不使用方法或类,就不可能在简单的脚本中创建这样的代码块。
The only thing, which comes close to collapsible block of codes is the code cell
feature from PyCharm. Find more informations about this here.
唯一接近可折叠代码块的是来自PyCharm的代码单元格功能。在这里可以找到更多关于这方面的信息。
Or you can use a dummy "if True:" like in:
或者,您可以使用虚拟的“If True:”,如下所示:
if True:
#now you are in a new block of code
#
Technically you can use brackets
从技术上讲,您可以使用方括号
def function():
{
print("Witam")
}
function()
And python interpreter can read this, but i would say it's not recomended and doesn't go along with pythonic way of working with code:D
而Python解释器可以读取这些内容,但我想说它不是推荐的,也不符合Python式的代码处理方式:d
更多回答
Okay. That is one of the things why I hate Python
, I would rather call Python
functions from R
using reticulate
package
好吧。这就是我讨厌Python的原因之一,我宁愿使用网格化程序包从R调用Python函数
我是一名优秀的程序员,十分优秀!