Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: classes

Reply
Thread Tools

Re: classes

 
 
Mark Lawrence
Guest
Posts: n/a
 
      10-24-2012
On 24/10/2012 13:48, 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. Right so x shouldnot be in the argument.
>>

> Fine
>
> *class Bag:
>> def __init__(self):
>> self.data = []*
>>
>>
>>> def add(self, x):
>>> self.data.append(x)
>>> def addtwice(self, x):
>>> self.add(x)
>>> self.add(x)


No return given so it will default to returning None, but...

>>> y = Bag(4)
>>>

>>
>> Create y with an argument of 4 'which is discarded in the initialiser.'
>> means ??
>>
>>
>> print " Adding twice of %4.2f gives " % (y.addtwice())


....you seem to be expecting addtwice to be returning a Python float.
Only you can tell us why as I've not yet gone to first year mind reading
classes

>>>

>>
>> There's no argument passed to addtwice here. ' why am I not passing y to
>> addtwice here ??


You are passing y, it's called self. Why aren't you passing x?

>>
>>
>>
>>>
>>> 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? I am learning
>> ....
>>
>>
>>
>>> why the prog is having this error with self nd x as arguments ???
>>>

>>
>> What x argument? Clearly wrong as I've pointed out above. How can i
>> correct it ??


Put whatever it is you want appended to self.data in the call to
y.addtwice. And/or get addtwice to return the correct data type.
And/or correct anything that I've missed like I did the first time around.

>>
>>
>> --
>> Cheers.
>>
>> Mark Lawrence.
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>

>


--
Cheers.

Mark Lawrence.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes within classes David ASP .Net 2 07-22-2005 07:13 PM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM
Modifiers applied to attributes, local variables, member functions, classes and inncer classes ! Razvan Java 11 07-17-2004 08:57 PM
Can I using reflection to get all child classes or classes undera package dynamically? Carfield Yim Java 1 05-31-2004 05:33 PM
How to access inner classes variables & methods from outer classes lonelyplanet999 Java 1 11-13-2003 01:54 PM



Advertisments