Python 语言的细微差别
可能的重复:
Python 中的常见陷阱
我正在学习 Python,并且来自不同的编程语言背景。在过去五年中,我编写了大量 Java、C++、VB.Net 和 PHP 代码。正如你们中许多人可能同意的那样,一旦您学习了一种编程语言,学习另一种编程语言只需了解语法和最佳实践的差异即可。
从 PHP 开始,我已经非常习惯了许多脚本风格的语言功能。例如,像这样的东西让我内心发痒:
# Retrieve the value from the cache; otherwise redownload.
if(!($value = $cache->get($key)))
# Redownload the value and store in the cache.
$cache->set($key, $value = redownload($key));
不过,Python 并不认为赋值是一个表达式。 OTOH,它确实支持像 in
构造这样的好东西,我认为这是有史以来最伟大的发明之一。 x in y
比 !empty($y[$x])
好得多。
我还应该注意哪些其他细微差别、“缺失”功能和性能瓶颈?我希望尽可能无缝地过渡到 Python 开发,并希望了解一些有助于缩短开发时间并消除反复试验的秘密。感谢您的见解!
Possible Duplicate:
Common Pitfalls in Python
I'm learning Python and I come from a diverse background of programming languages. In the last five years, I've written quite a bit of Java, C++, VB.Net, and PHP. As many of you might agree, once you learn one programming language, learning another is just a mater of learning the differences in syntax and best practices.
Coming down from PHP, I've become very accustomed to a lot of script-style language features. For instance, stuff like this tickles me insides:
# Retrieve the value from the cache; otherwise redownload.
if(!($value = $cache->get($key)))
# Redownload the value and store in the cache.
$cache->set($key, $value = redownload($key));
Python, though, doesn't consider assignment to be an expression. OTOH, it does support nice things like the in
construct, which I find to be one of the greatest inventions of all time. x in y
is so much nicer than !empty($y[$x])
.
What other nuances, "missing" features, and performance bottlenecks should I watch out for? I'm hoping to make as seamless a transition into Python development as possible, and hope to learn some of the secrets that will help to smooth out development time and eliminate trial and error. Your insight is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当我第一次在实际程序中遇到这个问题时,我花了几个小时才弄清楚:
函数的默认参数是一个可变的静态值。
这将打印
This one took me a few hours to figure out when I first encountered it in a real program:
A default argument to a function is a mutable, static value.
This will print
对于你的例子,通常的方法是这样的
For your example, the usual way would be something like this
线程不会做您认为它们会做的事情,并且可能不应该按照您习惯的方式使用它们。对于许多人来说,这是一个巨大的陷阱,特别是对于那些习惯了 Java 的人来说,其中的习惯是
子类化 Thread实现 Runnable 接口来执行异步工作,并且有并行运行线程的语言支持(在具有多个 CPU 核心的机器上)。一般来说,您可能根本不需要线程,而是子进程。请参阅我对问题的回答“Python 线程和性能?”。
(更一般而言,可能有更好的方法。)
Threads do not do what you think they do, and probably shouldn't be used how you're used to using them. This is a huge gotcha for many people, especially for those used to Java where the custom is to
subclass Threadimplement the Runnable interface to do asynchronous work, and where there is language support for running threads in parallel (on machines with multiple CPU cores).In general you probably don't want threads at all, but subprocesses. See my answer to the question "python threading and performace?".
(In more general, there might be a better way altogether.)
例外是你的朋友。
与 C 和 PHP 等使用返回值来指示错误的语言相比,Python 使用异常来中断程序,而不是让错误导致进一步的问题。
Exceptions are your friend.
In contrast with languages like C and PHP which use the return value to indicate errors, Python uses exceptions to interrupt the program instead of allowing the errors to cause further problems down the line.
Python 代码通常比类似 C 的代码快得多。
类似:
更好地写成:
或
Pythonic code is usually much faster than C-like code.
Something like:
is better written as:
or