[Tutor] Tutor Digest, Vol 115, Issue 27

Dino Bektešević ljetibo at gmail.com
Thu Sep 12 14:25:05 CEST 2013


> Date: Wed, 11 Sep 2013 18:10:18 +0530
> From: zubair alam <zubair.alam.jmi at gmail.com>
> To: tutor <tutor at python.org>
> Subject: [Tutor] class data member and objects of class in python
> Message-ID:
>         <CAGqeC75oz8g8d2ibBwiCDFdGQdb65SYtDXKqfwVj8c2bP1OQqA at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> i am learning how a __class__ data member behaves in python as compared to
> static data member in java, but following code is throwing error
>
>
> 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)
>
> 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())
>
> output:
> Traceback (most recent call last):
>   File "/home/scott/pythonfiles/core_python/pizza.py", line 10, in <module>
>     print "a pizza pls!! {}:".format(pizza_order.next())
> StopIteration
>
>
> don't know where i am doing mistake....any help will be appreciated... i
> have other questions on based on this class

Integers different from zero are considered to be True. So what you're
basically doing is:
      >>> pizza_stock=10
      >>> while not pizza_stock:0 ## not True == False

so the loop never runs, not even once. Python reports an error because
of that. Similar reports occur if you have variables that are not used
but those are Warnings and not actual Error events.

Regards,
Dino


More information about the Tutor mailing list