作为一名学习Python的Java程序员,我应该注意什么? [英] As a Java programmer learning Python, what should I look out for?

查看:125
本文介绍了作为一名学习Python的Java程序员,我应该注意什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的大部分编程背景都是用Java编写的,而且我仍在用Java编写大部分编程。但是,我开始在工作中学习一些侧面项目的Python,我想学习它尽可能独立于我的Java背景 - 即我不想只用Python编写Java。我应该注意哪些事情?

Much of my programming background is in Java, and I'm still doing most of my programming in Java. However, I'm starting to learn Python for some side projects at work, and I'd like to learn it as independent of my Java background as possible - i.e. I don't want to just program Java in Python. What are some things I should look out for?

一个简单的例子 - 在查看Python教程时,我发现了一个函数的默认可变参数(如作为列表)是持久的(记得通话)。这对我作为Java程序员来说是违反直觉的,很难让我理解。 (参见此处其他。)

A quick example - when looking through the Python tutorial, I came across the fact that defaulted mutable parameters of a function (such as a list) are persisted (remembered from call to call). This was counter-intuitive to me as a Java programmer and hard to get my head around. (See here and here if you don't understand the example.)

有人还向我提供了列表,我觉得有帮助,但很简短。任何人都有任何其他Java程序员可能会滥用Python的例子......?或者Java程序员会错误地假设或难以理解的东西?

Someone also provided me with this list, which I found helpful, but short. Anyone have any other examples of how a Java programmer might tend to misuse Python...? Or things a Java programmer would falsely assume or have trouble understanding?

编辑:好的,简要概述了文章I解决的原因链接以防止答案中的重复(如比尔蜥蜴建议)。 (如果我在措辞上犯了错误,请告诉我,我只是只是从Python开始,所以我可能完全不了解所有概念。而且免责声明 - 这些将是非常简短,所以如果你不明白它在检查链接时会得到什么。)

Edit: Ok, a brief overview of the reasons addressed by the article I linked to to prevent duplicates in the answers (as suggested by Bill the Lizard). (Please let me know if I make a mistake in phrasing, I've only just started with Python so I may not understand all the concepts fully. And a disclaimer - these are going to be very brief, so if you don't understand what it's getting at check out the link.)


  • 静态方法Java没有转换为Python类方法

  • Java中的switch语句转换为Python中的哈希表

  • 不要使用XML

  • 吸气剂和制定者是邪恶的(嘿,我只是引用:))

  • 代码重复通常是Java中必不可少的恶魔(例如方法重载),但不是在Python中

  • A static method in Java does not translate to a Python classmethod
  • A switch statement in Java translates to a hash table in Python
  • Don't use XML
  • Getters and setters are evil (hey, I'm just quoting :) )
  • Code duplication is often a necessary evil in Java (e.g. method overloading), but not in Python

(如果你觉得这个问题很有意思,那么请查看链接。:)这是非常好的。 )

(And if you find this question at all interesting, check out the link anyway. :) It's quite good.)

推荐答案


  • 不要把所有东西放到课堂上。 Python的内置列表和词典将带您走远。

  • 不要担心每个模块保留一个类。按目的划分模块,而不是按类划分。

  • 对行为使用继承,而不是接口。不要为Dog和Cat创建一个Animal类来继承,只是为了拥有一个通用的make_sound方法。

    • Don't put everything into classes. Python's built-in list and dictionaries will take you far.
    • Don't worry about keeping one class per module. Divide modules by purpose, not by class.
    • Use inheritance for behavior, not interfaces. Don't create an "Animal" class for "Dog" and "Cat" to inherit from, just so you can have a generic "make_sound" method.
    • 只需这样做:

      class Dog(object):
          def make_sound(self):
              return "woof!"
      
      class Cat(object):
          def make_sound(self):
              return "meow!"
      
      class LolCat(object):
          def make_sound(self):
              return "i can has cheezburger?"
      

      这篇关于作为一名学习Python的Java程序员,我应该注意什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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