Re: classes

Discussion in 'Python' started by Mark Lawrence, Oct 24, 2012.

  1. On 24/10/2012 13:11, inshu chauhan wrote:
    > I was just trying out a programme for learning classes in python
    >
    > The prog below is showing an error which it should not show :
    >
    > class Bag:
    > def __init__(self, x):
    > self.data = []


    You do nothing with x here.

    >
    > def add(self, x):
    > self.data.append(x)
    > def addtwice(self, x):
    > self.add(x)
    > self.add(x)
    > y = Bag(4)


    Create y with an argument of 4 which is discarded in the initialiser.

    > print " Adding twice of %4.2f gives " % (y.addtwice())


    There's no argument passed to addtwice here.

    >
    >
    > Error is :
    >
    > Traceback (most recent call last):
    > File "Z:\learning Python\learn5.py", line 35, in <module>
    > print " Adding twice of %4.2f gives " % (y.addtwice())
    > TypeError: addtwice() takes exactly 2 arguments (1 given)


    Exactly what I'd expect to happen. What did you expect?

    >
    > why the prog is having this error with self nd x as arguments ???


    What x argument? Clearly wrong as I've pointed out above.

    >
    >
    >


    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, 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:
    4,966
    David
    Jul 22, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,252
    VisionSet
    Nov 13, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,486
    Andrew Thompson
    May 31, 2004
  4. Razvan
    Replies:
    11
    Views:
    934
    Andrew Thompson
    Jul 17, 2004
  5. Razvan
    Replies:
    5
    Views:
    11,332
    Dale King
    Jul 27, 2004
Loading...

Share This Page