[Tutor] class data member and objects of class in python
Felix Dietrich
felix.dietrich at sperrhaken.name
Thu Sep 12 02:59:51 CEST 2013
> i am learning how a __class__ data member behaves in python as
> compared to static data member in java [...]
The error is not related to class variables. Also could you elaborate on
what you intended to find out with this snippet?
> class PizzaShop():
> pizza_stock = 10
>
> def get_pizza(self):
> while not PizzaShop.pizza_stock:
> PizzaShop.pizza_stock -= 1
> yield "take yours pizza order, total pizzas left {}".format(PizzaShop.pizza_stock)
The condition in the while loop is wrong. bool(PizzaShop.pizza_stock) is
True for all values but 0. (All numbers but 0 are considered True.) The
while loop does its commands while the condition holds True. When you do
not PizzaShop.pizza_stock
it will return False for values other than 0, therefor the loop is never
run and on exit of get_pizza it raises StopIteration to indicate that
there are no more values to be yielded.
> mypizza_shop = PizzaShop()
> pizza_order = mypizza_shop.get_pizza() # iterator is obtained
> print "a pizza pls!! {}:".format(pizza_order.next())
> print "a pizza pls!! {}:".format(pizza_order.next())
You might want to catch StopIteration here so that you can handle the
case that the shop runs out of the initial stack of pizzas. ;)
--
Felix Dietrich
More information about the Tutor
mailing list