我正在使用python中的类上的在线教程,然后我想尝试创建一个函数,在其他函数的返回中打印出值。这是代码:
from abc import ABCMeta, abstractmethod
class Vehicle(object):
__metaclass__ = ABCMeta
base_sale_price = 0
wheels = 0
def __init__(self, miles, make, model, year, sold_on):
self.miles = miles
self.make = make
self.model = model
self.year = year
self.sold_on = sold_on
def sale_price(self):
if self.sold_on is not None:
return 0.0
return 5000.0 * self_wheels
def purchase_price(self):
if self.sold_on is None:
return 0.0
return self.base_sale_price - (.10 * self.miles)
def spec(self):
x = self.sale_price
y = self.purchase_price
print "This car should sell at: ", x
print "This car should be bought at: ", y
@abstractmethod
def vehicle_type():
pass
class Car(Vehicle):
base_sale_price = 8000.0
wheel = 4
def vehicle_type(self):
return 'car'
class Truck(Vehicle):
base_sale_price = 10000.0
wheels = 4
def vehicle_type(self):
return 'truck'
AE86 = Truck(4, 35000, 'Toyota', 'AE86', 1986, )
print AE86.sale_price()
这就是我得到的:
This car should sell at: <bound method Truck.sale_price of <__main__.Truck object at 0x10666da50>>
This car should be bought at: <bound method Truck.purchase_price of <__main__.Truck object at 0x10666da50>>
我做错了什么?如何使方法的返回值显示?
答案 0 :(得分:0)
看起来你在def sale_price(self):
return 5000.0 * self.wheels
不是return 5000.0 * self_wheels
在def spec(self):
中你没有调用类方法。
def spec(self):
x = self.sale_price()
y = self.purchase_price()
print "This car should sell at: ", x
print "This car should be bought at: ", y