gpt4 book ai didi

python - 关于 "Python Is Not Java"的问题

转载 作者:太空狗 更新时间:2023-10-29 22:10:01 26 4
gpt4 key购买 nike

<分区>

我是一名具有基本 Java 经验的新手程序员,目前正在学习 Python。我在另一个问题线程中偶然发现了这篇博文:

http://dirtsimple.org/2004/12/python-is-not-java.html

我有几个关于发布主题的问题:


1) "Oh, and all those Foo.Bar.Baz attribute chains don't come for free, ... , so each dot counts. "

这个特定问题的解决方案是预先导入模块及其方法吗?如:

       from Foo.Bar import Baz
...
#now Baz() can be called directly without using Foo.Bar.Baz() everytime

2) Got a switch statement? The Python translation is a hash table, not a bunch of if-then statments.

关于这个主题有几个相关的答案,但他们也提出了几个问题:

  1. 使用 if-else 更简洁,但它没有 switch 语句中常数时间 O(1) 的优势。
  2. 在常数时间 O(1) 内使用哈希
  3. 在哈希中使用 lambda 函数进行比较(不推荐)
    • 为什么不推荐?是不是因为lambda函数去掉了hash的常数因子?
  4. 二分模块的使用
    • 此方法是否保留常数时间 O(1),还是只是另一种类型的 lambda 函数?
    • 那么Python中有什么方法,等于switch语句,时间复杂度O(1),同时允许比较语句?

3) Getters and setters are evil. Evil, evil...don't write getters and setters ... This is what the 'property' built-in is for ... In Python, this (getter and setter) is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class.

这部分我不是很明白。

此外,与 C++ 和 Java 相比,在 Python 中似乎可以轻松访问公共(public)和私有(private)方法或变量。这种行为是否有任何设计原因?


最后,关于 Python 与任何其他编程语言的比较,是否有任何推荐的好读物?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com