我正在研究Python教程并且我一直遇到TypeError:'Aircraft'对象无法调用
这是整个错误消息:
Traceback (most recent call last):
File "/usr/lib/python3.4/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "/home/johanvergeer/Documenten/Training/Pluralsight/Python- Fundamentals/airtravel.py", line 29, in aircraft
return self._aircraft()
TypeError: 'Aircraft' object is not callable
这是我正在处理的Python代码:
class Flight:
"""A flight with a particular passenger aircraft"""
def __init__(self, number, aircraft):
if not number[:2].isalpha():
raise ValueError("No airline code in '{}'".format(number))
if not number[:2].isupper():
raise ValueError("Invalid airline code '{}'".format(number))
if not (number[2:].isdigit() and 4 >= len(number[2:]) >= 3):
raise ValueError("Invalid route number '{}'".format(number))
self._number = number
self._aircraft = aircraft
rows, seats = self._aircraft.seating_plan()
self._seating = [None] + [{letter: None for letter in seats} for _ in
rows]
def number(self):
return self._number
def airline(self):
return self._number[:2]
def aircraft(self):
return self._aircraft()
def _parse_seat(self, seat):
"""Parse a seat designator into a valid row and letter.
Args:
seat: a seat designator such as 12F.
Returns:
A tuple containing an integer and a string for row and seat.
"""
row_numbers, seat_letters = self._aircraft.seating_plan()
letter = seat[-1]
if letter not in seat_letters:
raise ValueError("Invalid seat letter {}".format(letter))
row_text = seat[:-1]
try:
row = int(row_text)
except ValueError:
raise ValueError("Invalid seat row {}".format(row_text))
if row not in row_numbers:
raise ValueError("Invalid row number {}".format(row))
return row, letter
def allocate_seat(self, seat, passenger):
"""Allocate a seat to a passenger.
Args:
seat: A seat designator such as '12C' or '21F'.
passenger: The passenger name.
Raises:
ValueError: If the seat is unavailable.
"""
row, letter = self._parse_seat(seat)
if self._seating[row][letter] is not None:
raise ValueError("Seat {} already occupied".format(seat))
self._seating[row][letter] = passenger
def relocate_passenger(self, from_seat, to_seat):
"""Relocate a passenger to a different seat.
Args:
from_seat: The existing seat assigned to the passenger to be moved.
to_seat: The new seat designator.
"""
from_row, from_letter = self._parse_seat(from_seat)
if self._seating[from_row][from_letter] is None:
raise ValueError(
"No passenger to relocate in seat {}".format(from_seat))
to_row, to_letter = self._parse_seat(to_seat)
if self._seating[to_row][to_letter] is not None:
raise ValueError("Seat {} is already occupied".format(to_seat))
self._seating[to_row][to_letter] = self._seating[from_row][from_letter]
self._seating[from_row][from_letter] = None
def num_available_seats(self):
return sum(sum(1 for s in row.values() if s is None)
for row in self._seating
if row is not None)
def make_boarding_cards(self, card_printer):
for passenger, seat in sorted(self._passenger_seats()):
card_printer(passenger, seat, self.number(), self.aircraft())
def _passenger_seats(self):
""" An iterable series of passenger seating allocations"""
row_numbers, seat_letters = self._aircraft.seating_plan()
for row in row_numbers:
for letter in seat_letters:
passenger = self._seating[row][letter]
if passenger is not None:
yield (passenger, "{}{}".format(row, letter))
class Aircraft:
def __init__(self, registration, model, num_rows, num_seats_per_row):
self._registration = registration
self._model = model
self._num_rows = num_rows
self._num_seat_per_row = num_seats_per_row
def registration(self):
return self._registration
def model(self):
return self._model
def seating_plan(self):
return (range(1, self._num_rows + 1),
"ABCDEFGHJK"[:self._num_seat_per_row])
def make_flight():
f = Flight("BA758", Aircraft("G-EUPT", "Airbus A319", num_rows=22,
num_seats_per_row=6))
f.allocate_seat("12A", "Guido van Rossum")
f.allocate_seat("15F", "Bjorne Stroustrup")
f.allocate_seat("15E", "Anders Hejlsberg")
f.allocate_seat("1C", "John McCarthy")
f.allocate_seat("1D", "Richard Hickey")
return f
def console_card_printer(passenger, seat, flight_number, aircraft):
output = "| Name : {0}" \
" Flight : {1}" \
" Seat: {2}" \
" Aircraft : {4}" \
" |".format(passenger, flight_number, seat, aircraft)
banner = "+" + "-" * (len(output) - 2) + "+"
border = "|" + " " * (len(output) - 2) + "|"
lines = [banner, border, output, border, banner]
card = "\n".join(lines)
print(card)
print()
如果有人可以帮助我,我会非常感激的。
答案 0 :(得分:3)
问题出在您的班级Flight
:
def aircraft(self):
return self._aircraft()
self._aircraft()
尝试使用self._aircraft
作为函数,当它是Aircraft
类型的对象时。将您的Flight.aircraft
方法更改为
def aircraft(self):
return self._aircraft
这将返回实际对象。
答案 1 :(得分:1)
self._aircraft
是类Aircraft
的对象,返回时不需要调用它,只需直接返回它 -
def aircraft(self):
return self._aircraft