简体   繁体   中英

Python not creating a new clean instance?

So I'm creating a list of leaving flights in an array to iterate over in a later application.

I'm getting the information out of a long long html.

The xpathes check out and everything other is fine. I have a major wrapping object called FlightLegs. And a minor inner object called Flights.

The structure is simple. Every FlightLegs has a DATE on which the flights are leaving and THREE flights which are leaving on different TIMES ON that single DATE.

So like you have a date: 28/03/2012 and three flights which leave on : 8:10, 14:30, 20:00.

Simple enough.

So I have two nested loops. First, loops over FlightLegs. Sets DATE... then loops over the Flights in that Leg. And adds them to the array of Flights. Then ads the FlightLegs into a list. And begins a new.

Here is the code:

#Looping thourght the flight legs.
for flightLeg in flightLegs:

    #. is needed in the xpath so it matches items only in this flight leg.
    fleg = FlightLeg()
    fleg.Date = str(flightLeg.xpath(".//input[@name='departDate1']")[0].get('value'))
    innerFlights =  flightLeg.xpath(".//div[@class='flights_flight']")
    counter = 0
    #Getting the three flights in the flight leg leaving at 8 - 14 - 20.
    for flight in innerFlights:
        fl = Flight()
        fl.FlightPrice = str(flight.xpath(".//span[@class='flights_price']")[0].text)
        fl.FlightDepartureTime = str(flight.xpath(".//span[@class='flights_departuretime']")[0].text)
        fl.FlightArrivalTime = str(flight.xpath(".//span[@class='flights_arrivaltime']")[0].text)
        fl.FlightNumber = str(flight.xpath(".//span[@class='flights_flightnumber']")[0].text)
        fl.FlightDepAirport = str(flight.xpath(".//span[@class='flights_departureairport']")[0].text)

        fleg.Flights.append(fl)
        print "Lengts of inner flights: " + str(len(fleg.Flights))
        counter += 1
        print "Lengts of inner counter: " + str(counter)

    flightList.append(fleg)

Now the problem with this is that the flight legs Array in the inner flight is actually increasing over 3. Up until the last flight 600. It seems that no new object is created durring the loop... so what could cause this?

EDIT:

FlightLeg is:

class FlightLeg:
    Flight = []
    Date = ""

Flight is:

class Flight:
    FlightPrice = ""
    FlightDepartureTime = ""
    .
    .
    .

Btw.. I'm new to Python of course. I'm just learning. But now that read DSM-s comment, it might be that it is static after all. I thought variables will be public and instance by default.

How I would access them is not important. The important part is the counter. ;-)

I would access them through a foreach for example.

You need to initialize your variables (particularly the mutable ones) inside a constructor, rather than class-global. (You can get away with setting immutable types, such as strings or tuples of strings, inside the class definition, but in most cases probably shouldn't).

That is, instead of:

class FlightLeg:
    Flight = []

...which creates only a single Flight list, shared by all instances of FlightLeg ...

Do this:

class FlightLeg(object):
    def __init__(self):
        self.flight = []

...which creates a new flight list for every FlightLeg object.

(Using upper-case member names is not a bug, but happens to be unidiomatic in Python and contrary to PEP-8 ).

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