wxPython和宏(是:为什么人们不喜欢lisp? [英] wxPython and macros (was: Why don't people like lisp?

查看:74
本文介绍了wxPython和宏(是:为什么人们不喜欢lisp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tayss写道:


app = wxPySimpleApp()
frame = MainWindow(None,-1,A window)
frame.Show(True)
app.MainLoop()




为什么你需要一个宏?你为什么不写呢


def start_window(名字):

app = wxPySimpleApp()

frame = MainWindow (无,-1,名称)

frame.Show(True)

app.MainLoop()

return(app,frame)


宏用于其他东西!


有了这个功能,你可以做


app ,frame = start_window(" a window")

app.do_stuff()

frame.do_whatever()


随着宏观 (如果Python有它们),你可以定义

with_open_window(name),然后用/>
with_open_window(一个窗口)

app.do_stuff()

frame.do_whatever()


结论:这里你不需要宏。

道德:也许你不值得*宏。

解决方案



mi ***** @ ziplip.com 写道:

Tayss写道:

app = wxPySimpleApp()
frame = MainWindow(None,-1," A window")
frame.Show(True)
app.MainLoop( )



为什么你需要一个宏?你为什么不写呢

def start_window(名字):
app = wxPySimpleApp()
frame = MainWindow(无,-1,名字)
frame.Show(True)
app.MainLoop()
返回(app,frame)

宏用于其他事情!

这个功能,你可以做,

app,frame = start_window(一个窗口)
app.do_stuff()
frame.do_whatever()

使用宏 (如果Python有它们),你可以定义
with_open_window(name),然后用

with_open_window(一个窗口)
app.do_stuff()
frame.do_whatever()

结论:你不需要这里的宏。
道德:也许你不值得*宏。



他们不配? :)


你知道,我会再说一遍,考虑到观众(Pythonistas

在他们自己之间争论是否有宏)Lispniks应该有

从一开始就强调,宏的一个方面是清理

代码可视化(如上所示)。有些人认为我过度使用宏,但是我认为我之所以这么做是因为像Pythonistas一样,我在视觉上干净的代码上放了一个优惠的




如果你去这里:

http://alu.cliki.net/RtL%20Highlight%20Film


....并阅读Power Hungry小节。你会认为宏是

只是为了表达能力。关于权力的确是如此,但是有点奇怪的是,有些人的反应是我可以用HOF或元类来做

!好吧,它有点乱,但是......


问题是,它们是完全正确的!事实上,许多时髦:


(带沟槽(xyz)(做你的))


直接扩展进入:


(打电话给沟通(lambda(x)(做你的))

:选项-a y:选项-b z)


我,我不像一些Lispniks那么聪明,我发现视觉上的混乱

让我很难读甚至我自己的代码。在我生活的各个方面,我都是一个病态的邋b

,但是当涉及到代码时,我将从Oscar

切换到Felix(晦涩的奇怪的情侣参考)。如果代码看起来不那么好b $ b空气动力学效率我会削减角落,如果我知道粗略位是我正在构建的框架的关键部分,那么使用宏它将出现在数十个或数百个地方。
现在一个非常好的观点是宏是一个很大的变化来制作一个

看起来很干净的语言清洁工。也就是说,增加的价值可能不像在其他语言中那么好在Python中,因为另一种方式

(缩进)被发现来清理代码(根据我的经验,

改进是戏剧性的。


看看Pythonistas是否可以使用现有的Python功能清理你的wxPython

示例会很有趣。


如果是这样,好的,/现在/我们可以争论保罗格雷厄姆是否是正确的他/ b
表示有些事情只能通过宏:

http://www.paulgraham.com/ onlisp.html


:)


kenny the sophist

-
http://tilton-technology.com

什么?!你是新手而且你没有回答我:
http://alu.cliki.net/The%20Road%20to%20Lisp%20Survey


Kenny Tilton写道:< blockquote class =post_quotes>

mi*****@ziplip.com 写道:
< snip>


结论:你不需要宏。
道德:也许你不值得*宏。

他们不配? :)

你知道,我会再说一遍,考虑到观众(Pythonistas
在他们自己之间争论是否有宏)Lispniks应该从获得强调 - 宏观上做的事情之一是视觉清理代码(如上所示)。有些人认为我过度使用宏,但是我认为我之所以这么做是因为,像Pythonistas一样,我对视觉清晰的代码提供了优质的保障。




<剪断>现在一个非常好的观点是宏是一个很大的改变,使
看起来更干净的语言更清洁。也就是说,Python中的附加值可能不如其他语言那么好,因为发现另一种方式(缩进)来清理代码(根据我的经验,改进是戏剧性的。)




在讨论了这个主题之后,我对如何使用宏如果它们存在于python中的方式有​​了更好的理解

。我的想法是很多

很多涉及簿记和资源处理。


特别谈论wxPython,在某些类构造函数中,任何非

处理异常(例如,在wxFrame中)可能会使应用程序崩溃

而没有回溯。此外,当子类化一些wxPython

类时,基类*必须*是第一次调用。这些问题可能是以一种可能干净的方式用宏来解决的。当然,你不需要* b $ b *需要*它们,但它们确实减少了书籍 - 保持和视觉混乱我的

意见。


class wxFrame:

def __init __(self,...):

with wxFrame_init(self,...):

这里的特殊构造函数代码

而不是


class wxFrame:

def __init __(self,...):

wxFrame .__ init __(自我,参数)

试试:

这里的特殊构造函数代码

除了......:

#处理错误,也许写入日志

#用适当的关闭应用程序

#错误消息


也许我已经转换了:)我也认为这可以很好地适用于python环境中的
。比我以前要多得多。

我不会用宏来改变语言,而是提供故障保护

处理常见任务,这就是为什么我想先在

中使用python。


Brian





Brian Kelley写道:

Kenny Tilton写道:



mi ***** @ ziplip.com 写道:
< snip>


结论:你不需要这里的宏。
道德:也许你不值得*宏。



他们不值得? :)

你知道,我会再说一遍,考虑到观众(Pythonistas
在他们自己之间争论是否有宏)Lispniks应该从获得者强调去做宏的工作之一就是用视觉清理代码(如上所示)。有些人认为我过度使用了宏,但我认为我之所以这么做是因为,像Pythonistas一样,我非常重视视觉上干净的代码。



< ; snip>

现在一个非常好的观点是,宏是一个很大的改变,使
已经看起来干净整洁的语言。也就是说,Python中的附加值可能不如其他语言那么好,因为发现另一种方式(缩进)来清理代码(根据我的经验,改进是戏剧性的。)



在讨论这个话题之后,我对如何使用宏如果它们存在于python中有了更好的理解。我的想法非常好
很多处理记录和资源处理。

特别是在讨论wxPython时,在某些类构造函数中,任何非处理的异常(例如,wxFrame)都可能导致应用程序崩溃
没有追溯。另外,当继承一些wxPython类时,必须首先调用基类*。这些问题可以用宏可能以干净的方式解决。当然,你不能< * / *需要*他们,但他们确实减少了簿记和视觉混乱。

类wxFrame:
def __init __(self,...) :
wxFrame_i nit(self,...):
特殊的构造函数代码在这里

而不是

类wxFrame:
def __init __(self,.. 。):
wxFrame .__ init __(自我,参数)
尝试:
特殊的构造函数代码在这里
除了......:
#handle errors,也许写到一个log
#关闭应用程序并给出相应的
#错误消息




上面的意思是wxFrame3D,一个子类wxFrame?


无论如何......是的。只要有样板代码,就可以将它打成一个

宏。最后,只有不同的位被编码,因此它更加清晰,代码更容易阅读。


现在是百万美元的问题是:如果没有宏,使用HOF或元类黑客或其他东西清理上面的更长版本

怎么可能?


kenny


-
http://tilton-technology.com


为什么选择Lisp?...

http://alu.cliki.net/RtL%20Highlight%20Film


Tayss wrote:


app = wxPySimpleApp()
frame = MainWindow(None, -1, "A window")
frame.Show(True)
app.MainLoop()



Why do you need a macro for that? Why don''t you just write

def start_window(name) :
app = wxPySimpleApp()
frame = MainWindow(None, -1, name)
frame.Show(True)
app.MainLoop()
return (app, frame)

Macros are used for other things!

With this function, you can do

app, frame = start_window("a window")
app.do_stuff()
frame.do_whatever()

With a "macro" (if Python had them), you could define
with_open_window(name), and do

with_open_window("a window")
app.do_stuff()
frame.do_whatever()

Conclusion: you don''t need macros here.
Moral: maybe you do not *deserve* macros.

解决方案



mi*****@ziplip.com wrote:

Tayss wrote:

app = wxPySimpleApp()
frame = MainWindow(None, -1, "A window")
frame.Show(True)
app.MainLoop()


Why do you need a macro for that? Why don''t you just write

def start_window(name) :
app = wxPySimpleApp()
frame = MainWindow(None, -1, name)
frame.Show(True)
app.MainLoop()
return (app, frame)

Macros are used for other things!

With this function, you can do

app, frame = start_window("a window")
app.do_stuff()
frame.do_whatever()

With a "macro" (if Python had them), you could define
with_open_window(name), and do

with_open_window("a window")
app.do_stuff()
frame.do_whatever()

Conclusion: you don''t need macros here.
Moral: maybe you do not *deserve* macros.



They are unworthy? :)

You know, I''ll say it again, considering the audience (Pythonistas
debating amongst themselves whether to have macros) Lispniks should have
emphasized from the get-go that one of the things macros do is clean up
code visually (as demonstrated above). Some think I over-use macros, but
I think I do so precisely because, like Pythonistas, I place a premium
on visually clean code.

If you go here:

http://alu.cliki.net/RtL%20Highlight%20Film

....and read the subsection "Power Hungry" you would think macros are
there just for the expressive power. That''s true about the power, but
what is strange is that the response by some here has been "I can do
that with an HOF or a metaclass! OK, it''s a little messy, but...".

The problem is, they are precisely right! In fact, many a groovy:

(with-groove-on (x y z) (do-your-ting))

expands directly into :

(call-with-groove-on (lambda (x) (do-your-ting))
:option-a y :option-b z)

Me, I am not as smart as some Lispniks, and I find that visual clutter
makes it hard for me to read even my own code. I am a pathological slob
in all aspects of my life, but when it comes to code I switch from Oscar
to Felix (obscure "Odd Couple" reference). If code does not look
aerodynamically efficient I shave down the corners, with a macro if I
know the rough bit is such a key part of the framework I am building
that it will appear in dozens or hundreds of places.

Now one very good point is that macros are a big change to make an
already clean-looking language cleaner. ie, The added value may not be
so great in Python as in other languages because another way
(indentation) was found to clean up code (and in my experience the
improvement is dramatic.

It will be interesting to see if Pythonistas can clean up your wxPython
example using existing Python capabilities.

If so, ok, /now/ we can argue about whether Paul Graham is right when he
says that some things can only be done with a macro:

http://www.paulgraham.com/onlisp.html

:)

kenny the sophist
--
http://tilton-technology.com
What?! You are a newbie and you haven''t answered my:
http://alu.cliki.net/The%20Road%20to%20Lisp%20Survey


Kenny Tilton wrote:



mi*****@ziplip.com wrote:
<snip>


Conclusion: you don''t need macros here.
Moral: maybe you do not *deserve* macros.

They are unworthy? :)

You know, I''ll say it again, considering the audience (Pythonistas
debating amongst themselves whether to have macros) Lispniks should have
emphasized from the get-go that one of the things macros do is clean up
code visually (as demonstrated above). Some think I over-use macros, but
I think I do so precisely because, like Pythonistas, I place a premium
on visually clean code.



< snip> Now one very good point is that macros are a big change to make an
already clean-looking language cleaner. ie, The added value may not be
so great in Python as in other languages because another way
(indentation) was found to clean up code (and in my experience the
improvement is dramatic.



After debating this topic, I have a slightly better understanding of how
I would use macros if they existed in python. My thinking is pretty
much dealing with book-keeping and resource handling.

Specifially talking about wxPython, in some class constructors, any non
handled exception (in, for example, wxFrame) can crash the application
with no tracebacks. Additionally, when subclassing some wxPython
classes, the base class *must* be called first. These issues could be
solved with macros in a perhaps clean fashion. Naturally, you don''t
*need* them, but they do reduce book-keeping and visual clutter in my
opinion.

class wxFrame:
def __init__(self, ...):
with wxFrame_init(self, ...):
special constructor code here
as opposed to

class wxFrame:
def __init__(self, ...):
wxFrame.__init__(self, arguments)
try:
special constructor code here
except ...:
# handle errors, perhaps writing to a log
# close down the application with an appropriate
# error message

Perhaps I have become converted :) I also think that this could fit in
nicely to the python environment. Much more than I would have before.
I wouldn''t use macros to change the language, but to provide fail-safe
handling for common tasks, sort of why I wanted to use python in the
first place.

Brian




Brian Kelley wrote:

Kenny Tilton wrote:



mi*****@ziplip.com wrote:
<snip>


Conclusion: you don''t need macros here.
Moral: maybe you do not *deserve* macros.



They are unworthy? :)

You know, I''ll say it again, considering the audience (Pythonistas
debating amongst themselves whether to have macros) Lispniks should
have emphasized from the get-go that one of the things macros do is
clean up code visually (as demonstrated above). Some think I over-use
macros, but I think I do so precisely because, like Pythonistas, I
place a premium on visually clean code.


< snip>

Now one very good point is that macros are a big change to make an
already clean-looking language cleaner. ie, The added value may not be
so great in Python as in other languages because another way
(indentation) was found to clean up code (and in my experience the
improvement is dramatic.



After debating this topic, I have a slightly better understanding of how
I would use macros if they existed in python. My thinking is pretty
much dealing with book-keeping and resource handling.

Specifially talking about wxPython, in some class constructors, any non
handled exception (in, for example, wxFrame) can crash the application
with no tracebacks. Additionally, when subclassing some wxPython
classes, the base class *must* be called first. These issues could be
solved with macros in a perhaps clean fashion. Naturally, you don''t
*need* them, but they do reduce book-keeping and visual clutter in my
opinion.

class wxFrame:
def __init__(self, ...):
with wxFrame_init(self, ...):
special constructor code here
as opposed to

class wxFrame:
def __init__(self, ...):
wxFrame.__init__(self, arguments)
try:
special constructor code here
except ...:
# handle errors, perhaps writing to a log
# close down the application with an appropriate
# error message



Is the above meant to be for, say, wxFrame3D, a subclass of wxFrame?

Anyway...Yep. Any time there is boilerplate code one can slap it into a
macro. In the end only the bit that is different gets coded, so it
stands out more clearly and code is easier to read.

Now the million dollar question is: how could the longer version above
be cleaned up without macros, with HOFs or a metaclass hack or something?

kenny

--
http://tilton-technology.com

Why Lisp?...

http://alu.cliki.net/RtL%20Highlight%20Film


这篇关于wxPython和宏(是:为什么人们不喜欢lisp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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