Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: classes

 
 
Chris Angelico
Guest
Posts: n/a
 
      10-24-2012
On Thu, Oct 25, 2012 at 12:02 AM, inshu chauhan <(E-Mail Removed)> 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
 
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