简体   繁体   中英

Hold the class instances in a python list and iterate through each instance

I would like to create 4 instances of class Tire and hold the instances in a list ( All_Tires) and iterate (using for loop) through each instance and print the members of each instance.

class Tire():
    __identifier=0
    __temperature=0
    __pressure=0
    __pressure_position=0
    __temperature_position=0 
    __pressure_font=0
    __temperature_font=0
    __background_area =0    
    __color=0

    def __init__(self,fourbyteid,backarea,presspos,temppos,press=0,temp=0,bkcolor=GREEN):
        self.__identifier=fourbyteid
        self.__temperature=temp
        self.__pressure=press
        self.__pressure_position=presspos
        self.__temperature_position=temppos 
        self.__background_area=backarea
        self.__color=bkcolor

from tire import Tire

class ScreenArea(object):    
    def __init__(self,x,y):
        self.w=x 
        self.h=y

# Set the width and height of the screen [width, height]
ssize = ScreenArea(320,240)


FL = Tire("0d224bff",
          (0,0,ssize.w/2,ssize.h/2),
          (ssize.w*1/8, ssize.h/4),
          (ssize.w*1/8, ssize.h/4))
FR = Tire("0d224bf4",
          (ssize.w/2,0,ssize.w/2,ssize.h/2),
          (ssize.w*3/4, ssize.h/4),
          (ssize.w*3/4, ssize.h/4))
RL = Tire("0d2262b9",
          (0,ssize.h/2,ssize.w/2,ssize.h/2),
          (ssize.w*1/8, ssize.h*3/4),
          (ssize.w*1/8, ssize.h*3/4))
RR = Tire("0d22622a",
          (ssize.w/2,ssize.h/2,ssize.w/2,ssize.h/2),
          (ssize.w*3/4, ssize.h*3/4),
          (ssize.w*3/4, ssize.h*3/4))

All_Tires=[FL,FR,RL,RR]
print All_Tires
for tire in All_Tires:
    print tire.__pressure

I expect the print statement outputs a "0" instead get the error message "AttributeError: Tire instance has no attribute '__pressure'

An attribute name with two leading underscores (and no more than one trailing underscore) will be mangled , and subsequently become (mostly) inaccessible outside of the class definition. If you want to be able to access an attribute from anywhere, don't name it with two leading underscores.

If you change your class definition to:

class Tire():
    def __init__(self,fourbyteid,backarea,presspos,temppos,press=0,temp=0,bkcolor=GREEN):
        self.identifier=fourbyteid
        self.temperature=temp
        self.pressure=press
        self.pressure_position=presspos
        self.temperature_position=temppos 
        self.background_area=backarea
        self.color=bkcolor

And your print statement to:

for tire in All_Tires:
    print tire.pressure

... Then your output will be

[<__main__.Tire instance at 0x02986260>, <__main__.Tire instance at 0x02986288>, <__main__.Tire instance at 0x029862B0>, <__main__.Tire instance at 0x029862D8>]
0
0
0
0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM