classes

inshu chauhan insideshoes at gmail.com
Wed Oct 24 10:02:02 EDT 2012


ok.. This was an example i was trying to run from
http://docs.python.org/tutorial/classes.html   ...

On Wed, Oct 24, 2012 at 3:48 PM, Chris Angelico <rosuav at gmail.com> wrote:

> On Thu, Oct 25, 2012 at 12:02 AM, inshu chauhan <insideshoes at gmail.com>
> wrote:
> > I changed the programme to this :
> >     def addtwice(self, x):
> >          self.add(x)
> >          self.add(x)
> >          return x
> > y = Bag()
> > print y.addtwice(4)
> >
> > Now its not showing any error but result is same as the number passed for
> > adding twice ....
>
> Do you understand how function calls work? A function like
> "y.addtwice" is called with an argument of 4, and the return value
> from the function is the value of the expression.
>
> some_value = y.addtwice(4)
> print some_value
>
> Take the return value and put it in the place where the function call
> was. In this case, the return value is x, the number you passed in as
> an argument.
>
> What exactly do you expect addtwice to return? Should it return the
> bag object (self)? Should it return True to say that it's been added
> successfully (or False if there's an error)? Should it return the
> number of items in the bag? Should it return 0 for success and a
> nonzero error code for failure? Should it always return None, throwing
> an exception if anything goes wrong? All of these make sense, you just
> have to choose which one you want.
>
> (I'm handwaving away a lot of complexity here, like un/bound methods
> and member lookups. Bear with me. I'm also ignoring the fact that some
> things just aren't Pythonic. The bear isn't complaining about that, so
> nor should you.)
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121024/30044705/attachment.html>


More information about the Python-list mailing list