允许Python中的任意缩进 [英] Allowing Arbitrary Indentation in Python

查看:80
本文介绍了允许Python中的任意缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个朋友正在捡起一些Python,并且对于b
Python的缩进规则感到沮丧( http://greatbiggary.livejournal.com/

260460.html?thread = 1835884#t1835884)。就我个人而言,我从未遇到任何与Python缩进方式有关的问题,但那次谈话让我想到了这个问题。
br />
考虑以下缩进规则:

1.块以以:开头的行开头:

2.同一行缩进级别或下限结束一个块。


根据这些规则,这将起作用:

layouts = [''column'',''form'' ,''frame'']

cmds.window(t =''gwfUI Builder'')

cmds.paneLayout(configuration =''vertical3'',ps = ((1,25,100),(3,20,

100)))

cmds.paneLayout(configuration =''horizo​​ntal2'')

cmds.frameLayout(l =''布局'')

cmds.scrollLayout(cr = True)

cmds.columnLayout(adj = True,cat = ('''',2))

for i in layouts:

cmds.button(l = i)

cmds.setParent (''..'')

cmds.setParent(' ..'')

cmds.setParent(''..'')

cmds.setParent(''..'')

cmds.setParent(''..'')

cmds.showWindow()


这样的规则有意义吗?是否有任何方法可以使代码在Python中以这种方式工作?b
?应该有吗?是否会使这类代码变得更难或更难以理解?


P.S.我绝对不会在这里寻找标签与空间的火焰。

这是一个不同的问题。

解决方案

< blockquote> 12月18日下午2:16,Sam< L33tmin ... @ gmail.comwrote:


layouts = [''column'','' form'',''frame'']

cmds.window(t =''gwfUI Builder'')

cmds.paneLayout(configuration =''vertical3'' ,ps =((1,25,100),(3,20,

100)))

cmds.paneLayout(configuration =''horizo​​ntal2'')

cmds.frameLayout(l =''布局'')

cmds.scrollLayout(cr = True)

cmds.columnLayout(adj = True ,cat =('''',2))

for i in layouts:

cmds.button(l = i)

cmds.setParent(''..'')

cmds.setParent(''..'')

cmds.se tParent(''..'')

cmds.setParent(''..'')

cmds.setParent(''..'')

cmds.showWindow()



虽然格兰特正在拉出他的头发并在

月亮上大喊大叫,但让我来吧试着解释为什么你永远不想缩进代码那样的方式,更别说用这种方式编写代码了。


如果你必须要运行嵌套方式的一系列代码,像这样的
,最好创建函数然后嵌套函数。

当然,我在想更多的是一个lisp解决方案,而不是一个C的解决方案。


在这种情况下,你将不得不拥有对象(或数据

结构)知道如何执行必要的命令。

当你把它们全部组装好后,你只需以递归的方式运行它们。
$ br />
例如:


cmd.build((''pane'',dict(configuration =''horizo​​ntal) '),(''frame'',

dict(l =''Layouts''),(....))))


你可以任意缩进这些。另外,你不必担心匹配setParent和其他命令。


12月18日,7:09下午,Jonathan Gardner

< jgardner.jonathangardner .... @ gmail.comwrote:


12月18日下午2:16 ,Sam< L33tmin ... @ gmail.comwrote:


layouts = [''column'',''form'',''frame'']

cmds.window(t =''gwfUI Builder'')

cmds.paneLayout(configuration =''vertical3'',ps =((1,25,100) ),(3,20,

100)))

cmds.paneLayout(configuration =''horizo​​ntal2'')

cmds.frameLayout (l =''布局'')

cmds.scrollLayout(cr = True)

cmds.columnLayout(adj = True,cat =(''both'', 2))

for i in layouts:

cmds.button(l = i)

cmds.setParent(''..'')

cmds.setParent(''..'')

cmds.setParent(''..'')

cmds.setParent(''..'')

cmds.setParent(''..'')

cmds.showWindow()



虽然格兰特正在把他的头发拉出来并且在

月亮上大吼大叫,但让我试着解释你为什么不想要的缩进代码

方式,更不用说以这种方式编写代码了。


如果你必须以嵌套的方式运行一系列代码,<像这样的
,最好创建函数然后嵌套函数。

当然,我正在考虑更多的lisp解决方案而不是C在这种情况下,你将不得不拥有知道如何执必要的。

当你把它们全部组装起来时,你只需要以递归的方式运行它们r />
方式。


例如:


cmd.build((''pane'',dict(配置) =''水平''),(''框架'',

dict(l =''布局''),(....))))

您可以任意缩进这些。另外,你不必太担心匹配setParent和其他命令。



这对我有意义。在那个

例子中使用的库并没有让我觉得非常像Pythonic(使用一个

命令对象来处理所有事情并调用setParent('') ..'')指向

层次结构中的下一个对象)。但是,我没有理由(据我所知),他无法创造帮助函数来包裹它,因为

更加理智。


12月18日下午5:18,Sam< L33tmin ... @ gmail.comwrote:


On 12月18日,晚上7:09,Jonathan Gardner


< jgardner.jonathangardner .... @ gmail.comwrote:


12月18日下午2:16,Sam< L33tmin ... @ gmail.comwrote:


layouts = [''column'',''form'',''frame'']

cmds.window(t =''gwfUI Builder'')

cmds.paneLayout(configuration =''vertical3'',ps =((1,25,100),(3,20,

100)))

cmds .paneLayout(configuration =''horizo​​ntal2'')

cmds.frameLayout(l =''Layouts'')

cmds.scrollLayout (cr = True)

cmds.columnLayout(adj = True,cat =('''',2))

for i in layouts:

cmds.button(l = i)

cmds.setParent(''..'')

cmds.setParent(''..''')

cmds.setParent(''..'')

cmds.setParent(''..'')

cmds.setParent( ''..'')

cmds.showWindow()


格兰特拉他的头发在

月亮中大吼大叫,让我试着解释为什么你永远不想缩进代码那样的方式,更不用说以这种方式编写代码了。


如果你必须以嵌套方式运行一系列代码,

就像这样,它'''最好创建函数然后嵌套函数。

当然,我正在考虑更多的lisp解决方案而不是C语言解决方案。


在这种情况下,你将不得不拥有知道什么的对象(或数据

结构)要执行必要的命令。

当你把它们全部组装好后,你只需以递归的方式运行它们。


例如:


cmd.build((''' pane'',dict(configuration =''horizo​​ntal''),(''frame'',

dict(l =''Layouts''),(....))))


您可以任意缩进这些。另外,你不必太担心匹配setParent和其他命令。



这对我有意义。在那个

例子中使用的库并没有让我觉得非常像Pythonic(使用一个

命令对象来处理所有事情并调用setParent('') ..'')指向

层次结构中的下一个对象)。但是没有理由(据我所知),他无法创造帮助函数来包装它,而是更加理智。



让我补充一点,对于大量的GUI布局,大多数编程语言

都是错误的工具。 GUI布局涉及大量嵌套的,几乎是锅炉板类型的数据。使用工具生成代码可以加载的布局,并执行您可能需要的一些调整。

这使得事情变得更加清晰,特别是对于任何人必须

以后维护布局。


缺少这样的布局工具是GUI库的弱点,而不是

必然是程序语言的问题。


--Jason


A friend of mine is picking up some Python and is frustrated by
Python''s indentation rules (http://greatbiggary.livejournal.com/
260460.html?thread=1835884#t1835884). Personally, I''ve never had any
issues with Python''s ways of indentation, but that conversation got me
thinking about the issue.

Consider the following indentation rules:
1. Blocks begin with a line ending in ":"
2. A line on the same indentation level or lower ends a block.

Under those rules, this would work:
layouts = [''column'', ''form'', ''frame'']
cmds.window(t=''gwfUI Builder'')
cmds.paneLayout(configuration=''vertical3'', ps=((1, 25, 100), (3, 20,
100)))
cmds.paneLayout(configuration=''horizontal2'')
cmds.frameLayout(l=''Layouts'')
cmds.scrollLayout(cr=True)
cmds.columnLayout(adj=True, cat=(''both'', 2))
for i in layouts:
cmds.button(l=i)
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.showWindow()

Do such rules make sense? Is there any way to make code work that way
in Python? Should there be? Does that make this sort of code more or
less readable?

P.S. I''m definitely not looking for a tabs vs. spaces flamewar here.
That''s a different issue.

解决方案

On Dec 18, 2:16 pm, Sam <L33tmin...@gmail.comwrote:

layouts = [''column'', ''form'', ''frame'']
cmds.window(t=''gwfUI Builder'')
cmds.paneLayout(configuration=''vertical3'', ps=((1, 25, 100), (3, 20,
100)))
cmds.paneLayout(configuration=''horizontal2'')
cmds.frameLayout(l=''Layouts'')
cmds.scrollLayout(cr=True)
cmds.columnLayout(adj=True, cat=(''both'', 2))
for i in layouts:
cmds.button(l=i)
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.showWindow()

While Grant is pulling his hair out and yelling obscenities at the
moon, let me try to explain why you''d never want to indent code that
way, let alone write code that way.

In cases where you have to run a sequence of code in a nested way,
like this, it''s best to create functions and then nest the functions.
Of course, I''m thinking more of a lisp solution and less of a C one.

In this case, you''re going to have to have objects (or data
structures) that know what to do to execute the commands necessary.
When you get them all assembled, you simply run them in a recursive
way.

For instance:

cmd.build((''pane'', dict(configuration=''horizontal''), (''frame'',
dict(l=''Layouts''), (....))))

You can indent these arbitrarily. Plus, you don''t have to worry too
much about matching setParent and the other commands.


On Dec 18, 7:09 pm, Jonathan Gardner
<jgardner.jonathangardner....@gmail.comwrote:

On Dec 18, 2:16 pm, Sam <L33tmin...@gmail.comwrote:

layouts = [''column'', ''form'', ''frame'']
cmds.window(t=''gwfUI Builder'')
cmds.paneLayout(configuration=''vertical3'', ps=((1, 25, 100), (3, 20,
100)))
cmds.paneLayout(configuration=''horizontal2'')
cmds.frameLayout(l=''Layouts'')
cmds.scrollLayout(cr=True)
cmds.columnLayout(adj=True, cat=(''both'', 2))
for i in layouts:
cmds.button(l=i)
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.showWindow()


While Grant is pulling his hair out and yelling obscenities at the
moon, let me try to explain why you''d never want to indent code that
way, let alone write code that way.

In cases where you have to run a sequence of code in a nested way,
like this, it''s best to create functions and then nest the functions.
Of course, I''m thinking more of a lisp solution and less of a C one.

In this case, you''re going to have to have objects (or data
structures) that know what to do to execute the commands necessary.
When you get them all assembled, you simply run them in a recursive
way.

For instance:

cmd.build((''pane'', dict(configuration=''horizontal''), (''frame'',
dict(l=''Layouts''), (....))))

You can indent these arbitrarily. Plus, you don''t have to worry too
much about matching setParent and the other commands.

That makes sense to me. The library that was being used in that
example didn''t strike me as very Pythonic (what with the using one
command object for everything and calling setParent(''..'') to point to
the next object up in the hierarchy). But there''s no reason (as far
as I know) that he couldn''t create helper functions to wrap that with
something more sane.


On Dec 18, 5:18 pm, Sam <L33tmin...@gmail.comwrote:

On Dec 18, 7:09 pm, Jonathan Gardner

<jgardner.jonathangardner....@gmail.comwrote:

On Dec 18, 2:16 pm, Sam <L33tmin...@gmail.comwrote:

layouts = [''column'', ''form'', ''frame'']
cmds.window(t=''gwfUI Builder'')
cmds.paneLayout(configuration=''vertical3'', ps=((1, 25, 100), (3, 20,
100)))
cmds.paneLayout(configuration=''horizontal2'')
cmds.frameLayout(l=''Layouts'')
cmds.scrollLayout(cr=True)
cmds.columnLayout(adj=True, cat=(''both'', 2))
for i in layouts:
cmds.button(l=i)
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.setParent(''..'')
cmds.showWindow()

While Grant is pulling his hair out and yelling obscenities at the
moon, let me try to explain why you''d never want to indent code that
way, let alone write code that way.

In cases where you have to run a sequence of code in a nested way,
like this, it''s best to create functions and then nest the functions.
Of course, I''m thinking more of a lisp solution and less of a C one.

In this case, you''re going to have to have objects (or data
structures) that know what to do to execute the commands necessary.
When you get them all assembled, you simply run them in a recursive
way.

For instance:

cmd.build((''pane'', dict(configuration=''horizontal''), (''frame'',
dict(l=''Layouts''), (....))))

You can indent these arbitrarily. Plus, you don''t have to worry too
much about matching setParent and the other commands.


That makes sense to me. The library that was being used in that
example didn''t strike me as very Pythonic (what with the using one
command object for everything and calling setParent(''..'') to point to
the next object up in the hierarchy). But there''s no reason (as far
as I know) that he couldn''t create helper functions to wrap that with
something more sane.

Let me add that, for lots of GUI layout, most programming languages
are the wrong tool for the job. GUI layout involves tons of nested,
almost boiler-plate type data. Use a tool to generate the layout that
the code can load, and perform the few tweaks that you might need.
This makes things far more sane, especially for anyone who has to
maintain the layout later.

The lack of such a layout tool is a weakness of your GUI library, not
necessarily a problem with the program language.

--Jason


这篇关于允许Python中的任意缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆