Objects does not include methods in the list comprehension

狂风中的少年 提交于 2020-12-31 05:54:55

问题


This question is related to my previous question and Bill's response there.

I have a class named StrucData in subfile.py

class StrucData:  
    def __init__(self, name):
        self.name=name
    
    def loadData(self, size=1, cost=1):
        self.size=size  
        self.cost=cost
        return self

In the main file I:

  1. call the subfile,
  2. create a list of data names
  3. loop through the list to instantiate the objects; and
  4. load data using 'loadData' method for each object (I'm using the same 'size' and 'cost' to make this example easy.)

in one go using a list comprehension:

# in the main file

from subfile import StrucData 

listIndex=['data1','data2','data3']
listObjects = [StrucData(idx).loadData(size=3, cost=4) for idx in listIndex]

The output is

listObjects=[object1, object2, object3]

in which each object contains its attributes defined in the subfile.py (name, size, cost).

What I wonder is when I define one object using the same code as

x=StrucData(listIndex[0]).loadData(size=3, cost=4) 

it contains the method 'loadData' too.

Could anyone please explain to me why it happens?

I use anaconda3 distribution of Spyder, the version information is


回答1:


The inspector used when debugging your application inside Spyder treats objects inside lists differently from singular objects of the same type. It simply displays different things and - if in a list - omits function.

You can easily check if both objects have this method by printing it:

listIndex = ['data1','data2','data3']
listObjects = [StrucData(idx).loadData(size=3, cost=4) for idx in listIndex]

other = StrucData("other").loadData(size=3, cost=4) 

print(listObjects[0].loadData)
print(other.loadData)

You can assign one of the list elements to a normal variable and check its inspector output to verify:

lO = listObjects[1]

Set a breakpoint and inspect it - now the method shows up.

As to the why: ask the coders responsible for Spyder's debugging inspector code. As a hazarded guess: to save screen estate when displaying objects that are bundled inside a list.



来源:https://stackoverflow.com/questions/65036850/objects-does-not-include-methods-in-the-list-comprehension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!