0

I am new to object oriented programming and pytorch framework. I am stuck with the usage of syntax shown below:

self. variable_name=  some_class_name.apply

It would be great if someone can explain me this kind of syntax.

I tried searching this on various websites but could not find any appropriate solution. I saw the usage of this syntax when I was trying to understand the below code: https://github.com/liangqiyao990210/Quantum-Deep-Learning/blob/master/qiskit_demo/qiskit_demo.ipynb Thank you for great help.

mchaudh4
  • 65
  • 5
  • That's just an ordinary attribute lookup. The specific name `apply` has no special meaning here. – jasonharper Nov 16 '22 at 03:05
  • `self` is an object. `self.variable_name` is an attribute of that object. `some_class_name` is, presumably, a class object, and `some_class_name.apply` is an attribute of that class. I don't understand your confusion. – John Gordon Nov 16 '22 at 03:11
  • Thank you @jasonharper for your response. Can you explain a little about attribute lookup. – mchaudh4 Nov 16 '22 at 03:12
  • Thank you @JohnGordon for your response. It would be great if you can share some link where the usage of 'class_name.apply' is explained ? – mchaudh4 Nov 16 '22 at 03:36

1 Answers1

0

In python, assigning a function or method to a variable means that the assigned variable is a reference of the function or method. For example, we define a function func:

def func(x): 
    return x**2

and then we assign func to a variable g: g=func. After that, we can directly call the function func by calling g:

>>> g(3)
>>> 9
>>> func(3)
>>> 9

In other words, g is an alias of the function func.

In the link you've mentioned, self.qc = TorchCircuit.apply simply means that we create a reference of TorchCircuit.apply and assign it to self.qc. Calling self.qc(x) is totally equal to calling TorchCircuit.apply(x)

References:

https://www.geeksforgeeks.org/assign-function-to-a-variable-in-python/

Assigning a function to a variable

  • Thank you @Rancho Xia for your response. In the link I shared in the question, they write : qc = TorchCircuit.apply . Here TorchCircuit is class defined in the program. So, is this means we are creating a qc object of the class or does it mean anything different. Is apply some kind alias ? – mchaudh4 Nov 16 '22 at 03:30
  • We're definitely not creating an object. Creating an object should go something like this: `object = TorchCircuit()`. As you said, I think `self.qc = TorchCircuit.apply` creates some kind of alias. Maybe `TorchCircuit.apply` is a class method that could be called directly without creating instances and we wish to call it directly using `self.qc`. However, I didn't find the definition of `apply` either in the class `TorchCircuit` or in its parent class `Function`, so I'm not sure about how `apply` functions. The best way to figure it out is to run the code yourself and see what the `qc` is. – Rancho Xia Nov 16 '22 at 07:56
  • I checked the type of qc in the term qc = TorchCircuit.apply. It gives me the output as – mchaudh4 Nov 16 '22 at 19:05