[Tutor] list of instance objects, access attribute

Vincent Davis vincent at vincentdavis.net
Fri Jun 19 01:32:22 CEST 2009


given a class like
class B():
def __init__(self, b1, b2):
    self.fooa = fooa
    self.foob = foob

Ok now I have several instances in a list
b1 = B(1, 2)
b2 = B(3, 4)
b3 = B(9, 10)
alist = [b1, b2, b3]

Lets say for each instance of the class I want to print the value of
fooa if it is greater than 5. How do I do this, what I am unclear
about is how I iterate over the values of fooa. As I write this I am
thinking of trying
For x in alist:
    if x.fooa > 5 : print(x.fooa)

Is that the right way or is there a better?
will this work for methods?

Thanks
Vincent Davis


More information about the Tutor mailing list