Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32032

Re: classes

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: classes
Date 2012-10-24 13:41 +0100
References <CAFqGZRE1LbAgoOCAKuynYBw4Rj6++p+misZrsXmv1nwivHuNwg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2755.1351082335.27098.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: classes Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-24 13:41 +0100

csiph-web