PyCG icon indicating copy to clipboard operation
PyCG copied to clipboard

How to get the return value of a class or method?

Open AllenSun1024 opened this issue 3 years ago • 0 comments

Hi, @vitsalis

I wanna exploit PyCG to analyze the following code:

 def foo():
    pass

 class TensorSliceDataset:
    def __init__(self):
       pass
    def __call__(self, tensors, name):
       pass
    def repeat(self):
       pass
    def shuffle(self, buffer_size):
       pass

 class MyDataset:
    def __init__(self):
       pass
    def __bool__(self):
       return True
    @staticmethod
    def from_tensor_slices(tensors, name=None):
       foo()
       return TensorSliceDataset(tensors, name=name)

 tensor_data = [1., 2., 3.]
 dataset = MyDataset.from_tensor_slices(tensor_data)
 dataset = dataset.repeat().shuffle(buffer_size=100)

The corresponding result of PyCG is as follows:

{"dataset": ["dataset.TensorSliceDataset.repeat", "dataset.MyDataset.from_tensor_slices"], "dataset.foo": [], "dataset.TensorSliceDataset.__init__": [], "dataset.TensorSliceDataset.__call__": [], "dataset.TensorSliceDataset.repeat": [], "dataset.TensorSliceDataset.shuffle": [], "dataset.MyDataset.__init__": [], "dataset.MyDataset.__bool__": [], "dataset.MyDataset.from_tensor_slices": ["dataset.foo", "dataset.TensorSliceDataset.__init__"]}

As shown in the above result, two methods(foo and TensorSliceDataset.__init__) are called by MyDataset.from_tensor_slices. I want to distinguish TensorSliceDataset.__init__ which is returned by the caller from foo which is a normal method, how can I make it?


I've tried to check the value of return_ns and unfortunately found that it's not what I want, but node.value seems to be the returned method which is what I want. Can you give me some suggestions on distinguishing the returned method from other methods?

AllenSun1024 avatar Jan 13 '22 07:01 AllenSun1024