用类与类型声明类之间的区别? [英] Difference between declaring a class with class vs Type?

查看:77
本文介绍了用类与类型声明类之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据本教程(请参阅本段简单的元类使用),这两个声明之间是有区别的:

According to this tutorial (see the paragraph "Simple metaclass use"), There is a difference between the two declarations:

>>> MyClass = type('MyClass', (), {})
>>> MyClass
<class '__main__.MyClass'>

 >>> class Meta(type):
 ...     pass

关于区别,这就是教程说:

Concerning a difference, this is what the tutorial says:

class语句不仅是语法糖,它还执行了一些其他操作,例如设置了足够的 __ qualname __ __ doc __ 属性,或调用 __ prepare __

The class statement isn't just syntactic sugar, it does some extra things, like setting an adequate __qualname__ and __doc__ properties or calling __prepare__.

该教程是为python 3编写的,但是说在python 2不同的情况下将给出脚注。在本段中没有脚注

The tutorial is written for python 3, but says that footnotes will be given in the instances where python 2 differs. There are no footnotes to be found in this paragraph

我找不到其他说同样话的教程。事实上,我可以找到其他表示它们相同。在类型的阴暗面段落下,它说明了两个声明:

I cannot find other tutorials that say the same thing. As a matter of fact, I can find others saying that they are the same. Under the paragraph "The Darker Side of Type", it says about the two declarations:

我们可以通过以下操作达到完全相同的效果:

我提出这个问题的原因是,我偶然发现了似乎有所不同的情况。类声明起作用:

The reason for my question is that I have stumbled over a situation where there seems to be a difference. The class declaration works:

class Customers(Base):

     __table__ = ERPTables.Customers

虽然类型声明不起作用:

While the type declaration does not work:

 type('Customers', (Base, ), {'__table__': ERPTables.Customers})  

我一直在为最后保存最好的东西,但是当我说工作时,我的意思是在sqlAlchemy的上下文中。在下面的代码中使用时,类声明会产生结果。

I have been saving the best for last here, but when I say work, I mean in the context of sqlAlchemy. The class declaration yields a result when used in the code below

query = session.query(BasisOfAutoPayment_DK)

pd.read_sql(query.limit(5).statement, session.bind)

当使用完全相同的代码时,类型声明不起作用。

While the type declaration does not work when used in the exact same code.

这是sqlAlchemy,当然会发生很多事情,但是在两个实例中都使用了相同的对象。相同的基础对象,相同的表声明,相同的引擎。我以为差异就在那一边,但是我设法控制了这一点,而且我知道事实并非如此。这就是为什么我现在要询问这两个声明。

This being sqlAlchemy there is of course a lot going on, but the same objects have been used in both instances. The same Base object, the same Table declaration, the same engine. I thought that the difference were on that side, but I have tried to control for it, and I know believe that it isn't the case. That is why I am now inquiring about the two declarations.

编辑:
进一步检查这两个声明,我可以看到一些相似之处和不同之处。

Inpecting the two declarations further I can see some similarities and difference in behaviour.

相似之处:

在完成类声明之后。调用类名称将产生:

After doing the class declaration. Calling the Class name will produce:

>>> Customers
__main__.Customers

这与您在执行操作时得到的响应完全相同类型声明:

That is the exact same response as the one you get when doing the type declaration:

>>> type('Customers', (Base, ), {'__table__': ERPTables.Customers}) 
__main__.Customers

但这两者之间的最大区别是:

A big difference between the two however is:

在类声明之后,您可以调用 Customers 名称,并得到显示为 __ main __。Customers 的输出。

After the class declaration, you can call the Customers name and get the output just shown __main__.Customers.

但是在类型声明之后,调用 Customers 名称会出现名称错误:

After the type declaration however, a call to the Customers name you get a nameerror:

NameError: name 'Customers' is not defined

很明显,为什么两者在sqlAlchemy查询中的行为不同。难怪如果找不到对象,它就不能在类型版本中使用

Obviously why the two are acting differently in the sqlAlchemy query. No wonder it does not work in the type version, if the object cannot be found

推荐答案

这是个很棒的工具。
是的,这是一个非常不错的工具,因为使用 type可以传递任何参数,它将为您创建该对象。
可以说您可以使用consol输入来传递此参数或使用某种

This is great tool. yes, a realy great tool because using "type" you can pass any parameter and it will create that object for you.. let say you can use consol input to pass this parameter or use some sort of networking to call this function on a remote computer.

,但是要使用类,您必须对类类型进行硬编码,这可能会使程序的动态性降低。

but with "class" you have to hard code the the class types which might make our programm less dynamic.

所以它是构建动态和高级应用程序的好工具。

so it's a great utility for building dynamic and advanced applications.

这篇关于用类与类型声明类之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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