Re: classes

Discussion in 'Python' started by Chris Angelico, Oct 24, 2012.

  1. On Thu, Oct 25, 2012 at 12:02 AM, inshu chauhan <> 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
     
    Chris Angelico, Oct 24, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David

    Classes within classes

    David, Jul 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    5,004
    David
    Jul 22, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,286
    VisionSet
    Nov 13, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,544
    Andrew Thompson
    May 31, 2004
  4. Razvan
    Replies:
    11
    Views:
    947
    Andrew Thompson
    Jul 17, 2004
  5. Razvan
    Replies:
    5
    Views:
    11,365
    Dale King
    Jul 27, 2004
Loading...

Share This Page