问题:以Object为参数的类

我正在尝试将一些python代码转换为scala代码。所以我对Python完全陌生。

但是,为什么有些类将对象作为参数却从未明确使用它呢?首先将其作为参数的原因是什么?

例:

class Table(object)

感谢您的时间。

I’m trying to translate some python code to scala code. So I’m a total noob in Python.

But why do some classes have object as a parameter but never explicitly use it? What’s the reasoning for having it as a parameter in the first place?

Example:

class Table(object)

Thank you for your time.


回答 0

在Python2中,此方法声明Table为一种新型类(与“经典”类相对)。在Python3中,所有类都是新型类,因此不再需要。

新样式类具有一些经典类所缺少的特殊属性。

class Classic: pass
class NewStyle(object): pass

print(dir(Classic))
# ['__doc__', '__module__']

print(dir(NewStyle))
# ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

此外,属性超级不适用于经典类。

在Python2中,最好将所有类都设为新样式。(尽管为了向后兼容,标准库中的许多类仍然是经典类。)

通常,在诸如

class Foo(Base1, Base2):

Foo被声明为从基类Base1和继承的类Base2

object是Python所有类的母亲。它是一种新型的类,从这么继承object品牌Table新风格的类。

In Python2 this declares Table to be a new-style class (as opposed to “classic” class). In Python3 all classes are new-style classes, so this is no longer necessary.

New style classes have a few special attributes that classic classes lack.

class Classic: pass
class NewStyle(object): pass

print(dir(Classic))
# ['__doc__', '__module__']

print(dir(NewStyle))
# ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Also, properties and super do not work with classic classes.

In Python2 it is a good idea to make all classes new-style classes. (Though a lot of classes in the standard library are still classic classes, for the sake of backward-compatibility.)

In general, in a statement such as

class Foo(Base1, Base2):

Foo is being declared as a class inheriting from base classes Base1 and Base2.

object is the mother of all classes in Python. It is a new-style class, so inheriting from object makes Table a new-style class.


回答 1

Table类扩展了名为的类object。这不是参数。您可能要object显式扩展的原因是,它将类变成了新样式。如果您未明确指定它扩展object,直到Python 3,它将默认为旧类。(从Python 3开始,无论您是否显式扩展,所有类都是新样式object。)

有关新式和旧式类的更多信息,请参见此问题

The Table class is extending a class called object. It’s not an argument. The reason you may want to extend object explicitly is it turns the class into a new-style class. If you don’t explicitly specify it extends object, until Python 3, it will default to being an old-style class. (Since Python 3, all classes are new-style, whether you explicitly extend object or not.)

For more information on new-style and old-style classes, please see this question.


回答 2

请注意,“新样式”与“旧样式”类的区别特定于Python 2.x;在3.x中,所有类都是“新样式”。

Just a note that the “new-style” vs “old-style” class distinction is specific to Python 2.x; in 3.x, all classes are “new-style”.


回答 3

对于Python,类Table和类Table(object)相同。

它不是参数,而是从对象(与许多其他语言一样为基类)扩展的。

它只说它继承了“对象”中定义的任何内容。这是默认行为。

class Table and class Table(object) are no different for Python.

It’s not a parameter, its extending from object (which is base Class like many other languages).

All it says is that it inherits whatever is defined in “object”. This is the default behaviour.


回答 4

object是python中定义的类对象的最基本类型。对象的属性如下所示

** >>>目录(对象)

[‘ class ‘,’ delattr ‘,’ doc ‘,’ format ‘,’ getattribute ‘,’ hash ‘,’ init ‘,’ new ‘,’ reduce ‘,’ reduce_ex ‘,’ repr ‘,’ setattr ‘,’ sizeof ‘,’ str ‘,’ subclasshook ‘] **

所以Table(object)只是继承。

object is the most base type of class object defined in python. Attributes of object can be seen as below

**>>> dir(object)

[‘class‘, ‘delattr‘, ‘doc‘, ‘format‘, ‘getattribute‘, ‘hash‘, ‘init‘, ‘new‘, ‘reduce‘, ‘reduce_ex‘, ‘repr‘, ‘setattr‘, ‘sizeof‘, ‘str‘, ‘subclasshook‘]**

So Table(object) is just inheritance.!


回答 5

1)类名(对象):2)类名:它们都是相同的,但第一个类在编写方面相当好,在继承其他类到另一个类时看起来更好,看起来很同质。

一样吗?因此,Python中的所有事物都属于对象,这意味着Python中的所有事物都具有对象的属性,无论是否写入,它都会理解它。首先,我们明确地告诉第二个我们没有。

1)Class name (object): 2) class name: They both are same but first one quite better in terms of writing,it look better while inheriting other classes to another,i t looks homogeneous.

How same ? So,every thing in Python is come under object means every thing in Python has property of object,if write or don’t it will understand it. In first we explicitly tell it in second we didn’t.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。