Python: Error = Class 'Foo' has no 'bar' member? -
i receiving error:
attributeerror: type object 'shop' has no attribute 'inventory'
my class set:
class shop(object): def __init__(self, name, inventory, margin, profit): self.name = name self.inventory = inventory self.margin = margin self.profit = profit # initial inventory including 2 of each 6 models available inventory = 12 # markup of 20% on sales margin = .2 # revenue minus cost after sale bike in bikes.values(): profit = bike.cost * margin
and want print inventory:
print "mike's bikes has {} bikes in stock.".format(shop.inventory)
but keep getting same error. can make work with:
print "mike's bikes has %d bikes in stock." % (inventory)
but trying make switch .format()
you never created instance of class, shop.__init__()
method never run either.
your class doesn't have such attribute; attribute defined shop
class __init__
method itself.
create instance of class, attribute on instance:
# initial inventory including 2 of each 6 models available inventory = 12 # markup of 20% on sales margin = .2 # revenue minus cost after sale bike in bikes.values(): profit = bike.cost * margin bikeshop = shop("mike's bikes", inventory, margin, profit) print "mike's bikes has {} bikes in stock.".format(bikeshop.inventory)
in creating instance shop(....)
, python created instance , called __init__
method on instance. result, inventory
attribute added instance, , can access via bikeshop.inventory
.
Comments
Post a Comment