无法使用 Child class' object 访问 parent class' 全局变量
Unable to access parent class' global variable with Child class' object
我想在有人租车时更新可用汽车的计数器。当我尝试通过 child class 访问此变量时,它显示错误为“UnboundLocalError:赋值前引用的局部变量 'available_cars'”
PS: 还在努力中,所以我还没有完成所有的方法代码。
Parent Class
available_cars = 1000
class Car_rental():
def __init__(self):
pass
def display_available_cars(self):
print("Total avaialable cars for renting is:", available_cars)
def rent_hourly(self, cars_rented):
print("Our hourly rate is 0/hr.")
if cars_rented > available_cars:
print("Sorry! We currently do not have the number of cars requested. You can have {} cars for now if you want.".format(
available_cars))
elif cars_rented < available_cars:
print("Thank you for renting {} cars from Swift car renting portal. Enjoy you ride.".format(
cars_rented))
elif cars_rented < 0:
print ("Please provide a valid number of cars.")
def rent_weekly(self):
pass
def rent_monthly(self):
pass
def bill(self):
pass
def update_invetory(self, cars_rented):
available_cars = available_cars - cars_rented
Child Class
从汽车租赁进口 *
class 客户(Car_rental):
def init(self):
Car_rental.init(self)
def rent_cars(self):
mode = int(input(
"Please select the mode of renting the car:\n1. Hourly\n2. Weekly\n3. Monthly\n"))
if mode == 1 or mode == 'hourly' or mode == 'Hourly':
cars_rented = int(input("How many cars do you wish to rent?"))
self.rent_hourly(cars_rented)
self.update_invetory(cars_rented)
elif mode == 2 or mode == 'weekly' or mode == 'Weekly':
self.rent_weekly()
elif mode == 3 or mode == 'monthly' or mode == 'Monthly':
self.rent_monthly()
else:
print("Please provide appropriate input.")
def return_cars(self):
pass
因此,如果我正确地理解了你的问题,那么你的代码似乎有目标。
你想定义一个Car_Rentalclass,带有class级变量available_cars,初始设置为1000,然后作为客户租用车辆减少可用汽车的数量。
您还需要第二个客户 class 继承 Car_Rental class 实现计费功能。
虽然我不确定这一点,但您似乎还希望这些 class 定义中的每一个都驻留在它们自己单独的 python 脚本文件中。
这就是我解决这些问题的方式。
# If desired could be placed in separate file Car_Rental.py
class Car_Rental():
available_cars = 1000
def display_available_cars(self):
print("Total avaialable cars for renting is:", Car_Rental.available_cars)
def isOkayToRent(self, nmbr):
return nmbr <= Car_Rental.available_cars
def rent_hourly(self, cars_rented):
print("Our hourly rate is 0/hr.")
if cars_rented < 0:
print ("Please provide a valid number of cars.")
else:
if self.isOkayToRent(cars_rented):
print("Thank you for renting {} cars from Swift car renting portal. Enjoy you ride.".format(
cars_rented))
else:
print("Sorry! We currently do not have the number of cars requested. You can have {} cars for now if you want.".format(
Car_Rental.available_cars))
def rent_weekly(self):
pass
def rent_monthly(self):
pass
def bill(self):
pass
def update_inventory(self, nmbr):
assert nmbr > 0
Car_Rental.available_cars -= nmbr
@property
def cars(self):
return Car_Rental.available_cars
# Can be placed in second file.
#Note include this next line if classes are stored in separate files
#from Car_rental import Car_Rental
class Customer(Car_Rental):
def __init__(self):
Car_Rental.__init__(self)
def rent_cars(self):
mode = int(input(
"Please select the mode of renting the car:\n1. Hourly\n2. Weekly\n3. Monthly\n"))
if mode == 1:
cars_rented = int(input("How many cars do you wish to rent?"))
self.rent_hourly(cars_rented)
self.update_inventory(cars_rented)
elif mode == 2:
self.rent_weekly()
elif mode == 3:
self.rent_monthly()
else:
print("Please provide appropriate input.")
def return_cars(self):
pass
使用:
c1 = Customer()
c1.cars
1000
c1.rent_cars()
Please select the mode of renting the car:
1. Hourly
2. Weekly
3. Monthly
How many cars do you wish to rent? 2
Our hourly rate is 0/hr.
Thank you for renting 2 cars from Swift car renting portal. Enjoy you ride.
为了说明适当的库存减少:
c1.cars
产量 998
我想在有人租车时更新可用汽车的计数器。当我尝试通过 child class 访问此变量时,它显示错误为“UnboundLocalError:赋值前引用的局部变量 'available_cars'”
PS: 还在努力中,所以我还没有完成所有的方法代码。
Parent Class
available_cars = 1000
class Car_rental():
def __init__(self):
pass
def display_available_cars(self):
print("Total avaialable cars for renting is:", available_cars)
def rent_hourly(self, cars_rented):
print("Our hourly rate is 0/hr.")
if cars_rented > available_cars:
print("Sorry! We currently do not have the number of cars requested. You can have {} cars for now if you want.".format(
available_cars))
elif cars_rented < available_cars:
print("Thank you for renting {} cars from Swift car renting portal. Enjoy you ride.".format(
cars_rented))
elif cars_rented < 0:
print ("Please provide a valid number of cars.")
def rent_weekly(self):
pass
def rent_monthly(self):
pass
def bill(self):
pass
def update_invetory(self, cars_rented):
available_cars = available_cars - cars_rented
Child Class
从汽车租赁进口 *
class 客户(Car_rental): def init(self): Car_rental.init(self)
def rent_cars(self):
mode = int(input(
"Please select the mode of renting the car:\n1. Hourly\n2. Weekly\n3. Monthly\n"))
if mode == 1 or mode == 'hourly' or mode == 'Hourly':
cars_rented = int(input("How many cars do you wish to rent?"))
self.rent_hourly(cars_rented)
self.update_invetory(cars_rented)
elif mode == 2 or mode == 'weekly' or mode == 'Weekly':
self.rent_weekly()
elif mode == 3 or mode == 'monthly' or mode == 'Monthly':
self.rent_monthly()
else:
print("Please provide appropriate input.")
def return_cars(self):
pass
因此,如果我正确地理解了你的问题,那么你的代码似乎有目标。
你想定义一个Car_Rentalclass,带有class级变量available_cars,初始设置为1000,然后作为客户租用车辆减少可用汽车的数量。
您还需要第二个客户 class 继承 Car_Rental class 实现计费功能。
虽然我不确定这一点,但您似乎还希望这些 class 定义中的每一个都驻留在它们自己单独的 python 脚本文件中。
这就是我解决这些问题的方式。
# If desired could be placed in separate file Car_Rental.py
class Car_Rental():
available_cars = 1000
def display_available_cars(self):
print("Total avaialable cars for renting is:", Car_Rental.available_cars)
def isOkayToRent(self, nmbr):
return nmbr <= Car_Rental.available_cars
def rent_hourly(self, cars_rented):
print("Our hourly rate is 0/hr.")
if cars_rented < 0:
print ("Please provide a valid number of cars.")
else:
if self.isOkayToRent(cars_rented):
print("Thank you for renting {} cars from Swift car renting portal. Enjoy you ride.".format(
cars_rented))
else:
print("Sorry! We currently do not have the number of cars requested. You can have {} cars for now if you want.".format(
Car_Rental.available_cars))
def rent_weekly(self):
pass
def rent_monthly(self):
pass
def bill(self):
pass
def update_inventory(self, nmbr):
assert nmbr > 0
Car_Rental.available_cars -= nmbr
@property
def cars(self):
return Car_Rental.available_cars
# Can be placed in second file.
#Note include this next line if classes are stored in separate files
#from Car_rental import Car_Rental
class Customer(Car_Rental):
def __init__(self):
Car_Rental.__init__(self)
def rent_cars(self):
mode = int(input(
"Please select the mode of renting the car:\n1. Hourly\n2. Weekly\n3. Monthly\n"))
if mode == 1:
cars_rented = int(input("How many cars do you wish to rent?"))
self.rent_hourly(cars_rented)
self.update_inventory(cars_rented)
elif mode == 2:
self.rent_weekly()
elif mode == 3:
self.rent_monthly()
else:
print("Please provide appropriate input.")
def return_cars(self):
pass
使用:
c1 = Customer()
c1.cars
1000
c1.rent_cars()
Please select the mode of renting the car:
1. Hourly
2. Weekly
3. Monthly
How many cars do you wish to rent? 2
Our hourly rate is 0/hr.
Thank you for renting 2 cars from Swift car renting portal. Enjoy you ride.
为了说明适当的库存减少:
c1.cars
产量 998