简体   繁体   English

在Python中将原始数据类型名称作为函数参数传递

[英]passing primitive datatype name as function argument in python

I'm java programmer trying to learn Python and as a result I'm trying to relate both the languages. 我是试图学习Python的Java程序员,因此,我试图将这两种语言联系起来。

Recently I started working with PyQt5 library. 最近,我开始使用PyQt5库。 I'm following a tutorial on udemy where we used 我正在关注关于udemy的教程

custom_signal = pyqtSignal(str)

then in our slot the data comes in as string. 然后在我们的插槽中,数据作为字符串输入。 If we use 如果我们使用

custom_signal = pyqtSignal(int)

then data will be passed as an integer to slot. 然后数据将作为整数传递到插槽。

Could anyone explain me what's happening internally. 谁能解释我内部发生的事情。 Why we are passing datatype name to constructor of a class (pyqtSignal(int)). 为什么我们将数据类型名称传递给类的构造函数(pyqtSignal(int))。 Is it related to something like Generic types in java? 它与Java中的泛型类型有关吗?

List<Integer> list = new ArrayList<Integer>();

As the above variable only accepts integers, pyqtSignal(int) also is similar to that? 由于上述变量仅接受整数,因此pyqtSignal(int)也与此类似吗?

Could someone throw some light on that. 有人可以对此进行说明。 This may be silly, but I just wanted to understand whats happening inside. 这可能很愚蠢,但我只是想了解内部发生的事情。 I tried to watch the source code of pyqtSignal class but I was not able to find the same online anywhere. 我试图观看pyqtSignal类的源代码,但无法在任何地方在线找到相同的源代码。

Also could anyone please tell me object creation process that happens internally like what special methods (_new_, _init_ etc..) are called and in what sequence for what purpose. 也有人可以告诉我在内部发生的对象创建过程,例如调用什么特殊方法(_new _,_ init_等)以及为什么目的按什么顺序进行。

Thanks in advance 提前致谢

The pyqtSignal is in reality a factory that produces a new signal with the parameters you passed. pyqtSignal实际上是一家工厂,它会使用您传递的参数来产生新signal

The definition of the factory method is as follows: 工厂方法的定义如下:

PyQt5.QtCore.pyqtSignal(types[, name[, revision=0[, arguments=[]]]])

Meaning if you want to define a signal called rangeChanged that takes two integer arguments you'd do the below: 这意味着如果要定义一个名为rangeChanged的信号,该信号需要两个整数参数,则可以执行以下操作:

range_changed = pyqtSignal(int, int, name='rangeChanged')

You can read more about this factory here . 您可以在此处了解有关该工厂的更多信息。

In regards to the dunder methods ( __ name __ ) or otherwise known as magic methods - A good explanation can be found here . 关于dunder方法( __名称__ )或其他称为魔术方法-在这里可以找到很好的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM