1

I'm java programmer trying to learn Python and as a result I'm trying to relate both the languages.

Recently I started working with PyQt5 library. I'm following a tutorial on udemy where we used

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)). Is it related to something like Generic types in java?

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

As the above variable only accepts integers, pyqtSignal(int) also is similar to that?

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.

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.

Thanks in advance

Krishna
  • 601
  • 2
  • 9
  • 32

1 Answers1

2

The pyqtSignal is in reality a factory that produces a new signal with the parameters you passed.

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:

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.

Julian Camilleri
  • 2,975
  • 1
  • 25
  • 34
  • 1
    "types[, name[, revision=0[, arguments=[]]]]" what does this argument mean in python? is it a list? – Krishna Sep 07 '18 at 10:46
  • 1
    That's solely the function specification; and it's quite a wide industry used standard of specifying arguments that way. - Everything in the square brackets can be omitted; meaning they're optional. – Julian Camilleri Sep 07 '18 at 10:55
  • 1
    There's more explanation on that here if you want to read more about it: https://stackoverflow.com/questions/10053286/intx-base-square-brackets-in-functions-in-python-documentation – Julian Camilleri Sep 07 '18 at 10:57
  • 1
    In regards to dunder methods; it's quite a broad discussion; that can go on for days if not weeks. - For example: when inheriting a meta class the meta class __init__ is fired first and then the __call__ function; list goes on. – Julian Camilleri Sep 07 '18 at 11:01
  • Thanks Julian for the help. I'm googling about this (may be with wrong keywords) form so long time. Your answer helped me a lot. – Krishna Sep 07 '18 at 11:03
  • 1
    I'm glad it helped you buddy, if you need anything; let me know. :) - If it is the correct answer don't forget to mark it as the answer. – Julian Camilleri Sep 07 '18 at 11:09
  • @Krishna don't forget to accept the answer, by clicking on the very good next to the correct answer if you think it answered your question. Try not to leave any questions markes as unanswered. – Julian Camilleri Sep 07 '18 at 11:56
  • I have accepted the answer. Thanks a lot. Could you please edit the answer and add info on how pyqtSignal() creates signal specific to the datatype that we pass it. Some sample code explaining the internal logic is fine. – Krishna Sep 07 '18 at 12:47