Python 语言的细微差别

发布于 2024-09-09 01:20:04 字数 812 浏览 1 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拒绝两难 2024-09-16 01:20:04

当我第一次在实际程序中遇到这个问题时,我花了几个小时才弄清楚:

函数的默认参数是一个可变的静态值。

def foo(bar = []):
  bar.append(1)
  print(bar)

foo()
foo()

这将打印

[1]
[1, 1]

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.

def foo(bar = []):
  bar.append(1)
  print(bar)

foo()
foo()

This will print

[1]
[1, 1]
樱花坊 2024-09-16 01:20:04

对于你的例子,通常的方法是这样的

try:
    value = cache[key]
except KeyError:
    value = cache[key] = redownload(key)

For your example, the usual way would be something like this

try:
    value = cache[key]
except KeyError:
    value = cache[key] = redownload(key)
神也荒唐 2024-09-16 01:20:04

线程不会做您认为它们会做的事情,并且可能不应该按照您习惯的方式使用它们。对于许多人来说,这是一个巨大的陷阱,特别是对于那些习惯了 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 Thread implement 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.)

美人如玉 2024-09-16 01:20:04

例外是你的朋友。

与 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.

つ可否回来 2024-09-16 01:20:04

Python 代码通常比类似 C 的代码快得多。

类似:

new_list=[]
for i in xrange(len(old_list)):
   new_list.append(some_function(old_list[i]))

更好地写成:

new_list=[some_function(x) for x in old_list]

new_list=map(some_function,old_list)

Pythonic code is usually much faster than C-like code.

Something like:

new_list=[]
for i in xrange(len(old_list)):
   new_list.append(some_function(old_list[i]))

is better written as:

new_list=[some_function(x) for x in old_list]

or

new_list=map(some_function,old_list)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文