java Python 中的 JavaBean 等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14989100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:06:31  来源:igfitidea点击:

JavaBean equivalent in Python

javapythonjavabeans

提问by stealthspy

I am fairly new to using Python as a OOP. I am coming from a Java background. How would you write a javabean equivalent in python? Basically, I need a class that:

我对使用 Python 作为 OOP 还很陌生。我来自 Java 背景。你会如何在 python 中编写一个 javabean 等价物?基本上,我需要一个类:

  1. Implements serializable.
  2. Has getters and setters -> private properties
  3. dummy constructor
  1. 实现可序列化。
  2. 有 getter 和 setter -> 私有属性
  3. 虚拟构造函数

Any inputs? I am looking for a sample code!

任何输入?我正在寻找示例代码!

回答by

You don't, because Python is not Java. Most likely you should just write a less trivial class, construct a namedtuple, pass a dictionary, or something like that. But to answer the question:

你没有,因为Python 不是 Java。很可能您应该只编写一个不太重要的类,构造一个namedtuple,传递一个字典,或者类似的东西。但是要回答这个问题:

  1. Neither serializablenor "implementing an interface" makes sense in Python (well, in some frameworks and advanced use cases it does, but not here). Serialization modules, such as pickle, work without implementing or inheriting anything special (you can customize the process in other ways, but you almost never need to).
  2. You don't write getters and setters. You just use public attributes. Should you later require a nontrivial getter/setter, you can turn it into a propertytransparently.
  3. There's no need for a dummy constructor, unless you want to create the attributes and set default values for them. But that's probably a bad idea (for a bean-ish class), as not assigning values to those attributes is most likely an error, and dubious even when it isn't. So just let Python catch those errors for you (it raises AttributeErrorwhen a non-existent attribute is accessed).
  1. serializable在 Python 中,“实现接口”和“实现接口”都没有意义(嗯,在某些框架和高级用例中是这样,但在这里不是)。序列化模块,例如pickle,无需实现或继承任何特殊内容即可工作(您可以通过其他方式自定义流程,但您几乎不需要这样做)。
  2. 你不写 getter 和 setter。您只需使用公共属性。如果您稍后需要一个重要的 getter/setter,您可以将其转换为property透明的。
  3. 不需要虚拟构造函数,除非您想创建属性并为它们设置默认值。但这可能是一个坏主意(对于 bean-ish 类),因为不为这些属性赋值很可能是一个错误,即使不是,也很可疑。因此,只需让 Python 为您捕获这些错误(AttributeError当访问不存在的属性时会引发该错误)。

回答by miku

  1. You can serialize most object via the picklemodule;
  2. There are no such things as private attributes in Python (see also:
  3. Classes that do not define a constructor will use a default (according to the method resolution order).
  1. 您可以通过pickle模块序列化大多数对象;
  2. Python 中没有诸如私有属性之类的东西(另请参阅:
  3. 未定义构造函数的类将使用默认值(根据方法解析顺序)。

Example for constructor 'chain':

构造函数“链”的示例:

>>> class A(object):
...     def __init__(self):
...         print("A")
...     
... 
>>> class B(A): pass # has no explicit contructor
... 
>>> b = B()
A
>>> 

And - as @delnanwrote - you might want to read: http://dirtsimple.org/2004/12/python-is-not-java.html-- Java and Python have quite different cultures, it takes some time to dive into (and appreciate) both.

而且 - 正如@delnan所写 - 您可能想阅读:http: //dirtsimple.org/2004/12/python-is-not-java.html- Java 和 Python 的文化完全不同,需要一些时间来潜水进入(并欣赏)两者。

Also, after writing some code, it might be helpful to compare it to common idioms, as listed here (I certainly learned a lot this way):

此外,在编写了一些代码之后,将其与此处列出的常见习语进行比较可能会有所帮助(我当然通过这种方式学到了很多东西):

回答by Sebastian Wagner

Well, I'd think that data classeswould be similar to Java beans and that using them is actually a good idea, as it removes boiler plate.

嗯,我认为数据类将类似于 Java bean,使用它们实际上是一个好主意,因为它删除了样板。

回答by Jakob Bowyer

Implements serializable.

实现可序列化。

Pick your favorite format, and write a function that will serialize it for you. JSON, Pickle, YAML, any work. Just decide!

选择你最喜欢的格式,并编写一个函数来为你序列化它。JSON、Pickle、YAML,任何工作。就决定吧!

Has getters and setters -> private properties

有 getter 和 setter -> 私有属性

We don't do that here, those are attributes of bondage languages, we are all adults in this language.

我们在这里不这样做,那些是束缚语言的属性,我们都是这种语言的成年人。

dummy constructor

虚拟构造函数

Again not something we really worry about as our constructors are a little bit smarter than other languages. So you can just define one __init__and it can do all your initialization, if you must then write a factory or subclass it.

再次不是我们真正担心的事情,因为我们的构造函数比其他语言更聪明一点。所以你可以只定义一个__init__,它可以完成你所有的初始化,如果你必须编写一个工厂或子类。

回答by flaviotruzzi

As pointed by miku:

正如 miku 所指出的:

  1. Objects can be serialized by picke module, but there is not an interface to be implemented, Python is not Java.

  2. There is no private attribute in python, usually people use bar(the underscore) to mean private attributes, but they can be accessed from external world. Getters and setters are waste of time of both CPU and programmers.

  3. Nothing to add to miku answer.

  1. 对象可以通过picke模块序列化,但是没有实现接口,Python不是Java。

  2. python中没有私有属性,通常人们使用bar(下划线)来表示私有属性,但它们可以从外部世界访问。getter 和 setter 是 CPU 和程序员的浪费时间。

  3. 没有什么可添加到 miku 答案。

about properties: Real world example about how to use property feature in python?

关于属性:关于如何在 python 中使用属性功能的真实世界示例?

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

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