Accessing attributes of a list of objects in python -


i have list of objects, known bricks, want check attributes of objects in list. tried:

pos = [brick.start_pos brick in self.brick_list] 

to create list of required attribute i.e start_pos, populates list attribute of last brick in brick_list. using python 3.4. need lot of different attributes comprehension makes sense me.

brick class holder data , defined:

class brick:     def __init__(self, start_pos=0, current_pos=0, variety=0, moveability=0,                   neighbours=0, max_range=0, sides=0, rotation=0, length=0,                  previous_pos =0):         self.start_pos = start_pos         self.current_pos = current_pos         self.moveability = moveability         self.variety = variety         self.neighbours = neighbours         self.max_range = max_range         self.sides = sides         self.rotation = rotation         self.length = length         self.previous_pos = previous_pos 

with couple simple functions.

it appears list contains same brick instance, repeated n times (n being length of lister)

hell.brick_list = len(lister)*[brick()]  

instead, might want build list as

hell.brick_list = [brick() _ in lister]  

obviously, need pass constructor necessary parameters, otherwise end-up bricks identical attributes (default values).

actually, given second comment

for in range (len(lister)): hell.brick_list[i].start_pos = lister[i] 

the best option create , initialize list in 1 go

hell.brick_list = [brick(start_pos=pos) pos in lister] 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -