[Tutor] Tutor Digest, Vol 115, Issue 28

Jerry Val jreh.val at gmail.com
Sun Jan 12 01:17:01 CET 2014


I am trying to perfect myself in creating functions and looping, am using
python 3,can you help me with a few basic tips so i can create my own
functions and loops without making random mistakes?!
 On Sep 12, 2013 11:19 PM, <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>         tutor-request at python.org
>
> You can reach the person managing the list at
>         tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>    1. Re: class data member and objects of class in python
>       (Felix Dietrich)
>    2. Re: class data member and objects of class in python (zubair alam)
>    3. Re: class data member and objects of class in python (Alan Gauld)
>    4. Re: Tutor Digest, Vol 115, Issue 27 (Dino Bekte?evi?)
>    5. Re: class data member and objects of class in python (Dave Angel)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Thu, 12 Sep 2013 02:59:51 +0200
> From: Felix Dietrich <felix.dietrich at sperrhaken.name>
> To: tutor at python.org
> Subject: Re: [Tutor] class data member and objects of class in python
> Message-ID: <87hadqx3aw.fsf at sperrhaken.name>
> Content-Type: text/plain
>
> > 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
>
>
> ------------------------------
>
> Message: 2
> Date: Thu, 12 Sep 2013 14:40:08 +0530
> From: zubair alam <zubair.alam.jmi at gmail.com>
> To: Marc Tompkins <marc.tompkins at gmail.com>
> Cc: tutor <tutor at python.org>
> Subject: Re: [Tutor] class data member and objects of class in python
> Message-ID:
>         <
> CAGqeC76+uN1_+yw3UC78f10nJ8usZDPGTd90JnBkUXpo2BaD1g at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> class PizzaShop():
>     pizza_stock = 10
>     def get_pizza(self):
>         while 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()
> # print "{}".format(repr(pizza_order.next()))
>
> for order in pizza_order:
> print "{}".format(repr(order))
>
> domino_pizza_store = mypizza_shop.get_pizza()
> print "{}".format(repr(domino_pizza_store.next()))
>
> mypizza_shop.pizza_stock = 10
>
> domino_pizza_store = mypizza_shop.get_pizza()
> print "{}".format(repr(domino_pizza_store.next()))
>
>
> can't we again use the same object mypizza_shop once its generator is
> exhausted
>
>
> On Thu, Sep 12, 2013 at 6:53 AM, Marc Tompkins <marc.tompkins at gmail.com
> >wrote:
>
> > On Wed, Sep 11, 2013 at 5:40 AM, zubair alam <zubair.alam.jmi at gmail.com
> >wrote:
> >
> >> 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
> >>
> >>
> >
> > Change "while not PizzaShop.pizza_stock:" to "while
> > PizzaShop.pizza_stock:"; I get the following output:
> >
> >> a pizza pls!! take yours pizza order, total pizzas left 9:
> >> a pizza pls!! take yours pizza order, total pizzas left 8:
> >>
> >
> >
> >
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20130912/51dbe3d1/attachment-0001.html
> >
>
> ------------------------------
>
> Message: 3
> Date: Thu, 12 Sep 2013 14:39:53 +0100
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] class data member and objects of class in python
> Message-ID: <l0sg71$qau$1 at ger.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 12/09/13 10:10, zubair alam wrote:
> > class PizzaShop():
> >      pizza_stock = 10
> >      def get_pizza(self):
> >          while 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()
> >
> > for order in pizza_order:
> > print "{}".format(repr(order))
>
> You might as well just use
>
> print order
>
> > domino_pizza_store = mypizza_shop.get_pizza()
> > print "{}".format(repr(domino_pizza_store.next()))
> >
> > mypizza_shop.pizza_stock = 10
>
> This preobably isn't doing what you think it is.
> This is creating a new instance attribute in the
> mypizza_shop instance it is not resetting the
> class attribute. For that you would need to use
>
> PizzaShop.pizza_stock = 10
>
> > can't we again use the same object mypizza_shop
>  > once its generator is exhausted
>
> You can't use the same iterator again but you can
> get a new one. But your problem here is that you have
> not reset the class stock level.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
>
>
> ------------------------------
>
> Message: 4
> Date: Thu, 12 Sep 2013 14:25:05 +0200
> From: Dino Bekte?evi? <ljetibo at gmail.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Tutor Digest, Vol 115, Issue 27
> Message-ID:
>         <CAMGeA2UUmC9N=
> T8thyKNNmsS1K6A7C1RwHDiaxz4_oc2G7OV3A at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> > 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
>
>
> ------------------------------
>
> Message: 5
> Date: Thu, 12 Sep 2013 20:15:11 +0000 (UTC)
> From: Dave Angel <davea at davea.name>
> To: tutor at python.org
> Subject: Re: [Tutor] class data member and objects of class in python
> Message-ID: <l0t7cd$uk5$1 at ger.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1
>
> On 12/9/2013 05:10, zubair alam wrote:
>
>
> > <div dir="ltr"><div>class PizzaShop():</div><div>? ? pizza_stock =
> 10</div><div>? ? def get_pizza(self):</div><div>? ? ? ? while
> PizzaShop.pizza_stock:</div><div>? ? ? ? ? ? PizzaShop.pizza_stock -=
> 1</div><div>? ? ? ? ? ? yield "take yours pizza order, total pizzas
> left {}".format(PizzaShop.pizza_stock)</div>
> > <div><br></div><div>mypizza_shop = PizzaShop()</div><div>pizza_order =
> mypizza_shop.get_pizza()?</div><div># print
> "{}".format(repr(pizza_order.next()))</div><div><br></div><div>for
> order in pizza_order:</div><div>
> > <span class="" style="white-space:pre">       </span>print
> "{}".format(repr(order))</div><div><br></div><div>domino_pizza_store
> = mypizza_shop.get_pizza()</div><div>print
> "{}".format(repr(domino_pizza_store.next()))</div>
> > <div><br></div><div>mypizza_shop.pizza_stock =
> 10</div><div><br></div><div>domino_pizza_store =
> mypizza_shop.get_pizza()</div><div>print
> "{}".format(repr(domino_pizza_store.next()))</div><div><br></div><div><br>
> > </div><div>can't we again use the same object mypizza_shop once its
> generator is exhausted</div></div><div class="gmail_extra"><br><br><div
> class="gmail_quote">On Thu, Sep 12, 2013 at 6:53 AM, Marc Tompkins <span
> dir="ltr"><<a href="mailto:marc.tompkins at gmail.com" target="_blank">
> marc.tompkins at gmail.com</a>></span> wrote:<br>
>
> Please use text email, not hmtl.  The indentation of your program was
> messed up in places, and I can't tell whether it was you or the email
> program that messed it up.  This is a text newgroup, and html doesn't
> work reliably.
>
> As Alan has said, you're confusing class attributes with instance
> attributes.  But I wonder if the mistake is actually the reverse of what
> he says.
>
> Do you intend that an instance of PizzaShop refers to a particul pizza
> shop, and that it has its own inventory, independent of all other pizza
> shop instances?  in that case all use of the class attribute
> pizza_stock = 10 is bogus.
>
> To re-iterate the other point Alan made, an iterator is *required* to
> continue to throw Stopiteration once its exhausted and has thrown it
> once, even if new items show up that it could have used.
>
> But you can readily make a new iterator from the same mypizza_shop, once
> you set its pizza_stock to a nonzero value.
>
> --
> DaveA
>
>
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 115, Issue 28
> **************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140111/54e49318/attachment-0001.html>


More information about the Tutor mailing list