Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here's the thing: My program is a GUI-based calculator written in python/GTK. In the first version I didn't used classes, so here is a piece of the old code:

def show(result):
     textbox3.set_text( str(result) )
     (...) # Update counters, etc.

def on_button_pressed(*args):
    input_user = inputbox.get_text() 
    (...) # parsing of the input
    show( eval( input_user ) )

For instance, if I type on inputbox "12+3" and press the button, textbox3 show the result "15".

I've modified my project to use OOP. Here's the modified code:

class App:
    (...)

    def show(self,result):
         self.textbox3.set_text( str(result) )
         (...) # Update counters, etc.

    def on_button_pressed(self,*args):
        input_user = self.inputbox.get_text() 
        (...) # parsing of the input
        print input_user
        self.show( eval( input_user ) )

With this code, the textbox3 show the result "<app.c12app.App instance at 0x272e128>". Which mistake am I making here?

P.S.: The real code is too large, the parsing section is about 50 lines large. I added a line print input_user to proof that the parsing don't overwrite the variable input_user. The console prints the expression parsed (a string) correctly. But when I use the eval function in this string it returns an object, instead the numeric value of the expression.

share|improve this question
1  
What is input_user? Paste the rest of your on_button_pressed method. I have a feeling you may be overwriting input_user somewhere. –  Blender May 21 '13 at 0:17
1  
you are not calling the method self.show() here. You are still calling a function show(); no idea what might invoke App().show(), but it is not in the code you show here. –  Martijn Pieters May 21 '13 at 0:23
    
The output you show look like you were instead passing in self explicitly somewhere: self.show(self). –  Martijn Pieters May 21 '13 at 0:24
    
In other words: the code you added in the question wouldn't work, and if it did, would not show the result you shared with us. Please share your real code. –  Martijn Pieters May 21 '13 at 0:24
    
The real code is too large, the parsing section is about 50 lines large. I modified the self.show (it was wrong, indeed) and added a line print input_user to proof that the parsing don't overwrite the variable input_user. The console prints the expression parsed (a string) correctly. But when I use the eval function in this string it returns an object, instead the numeric value of the expression. –  David Sousa May 21 '13 at 1:00
add comment

2 Answers

It could be a couple things. First off, don't do class App(): in your definition, do class App:, and make sure you instantiate like foo = App() and not foo = App.

share|improve this answer
    
No, it's not that. I changed the code and nothing changes. –  David Sousa May 21 '13 at 1:07
    
Ok. This string <app.c12app.App instance at 0x272e128> represents an instance of one of your App objects (in memory), and the code/behavior can't really lie. Somehow you are not evaluating something as simple as 12+3. Since you're parsing for 50 lines anyway, you may as well find a way to get rid of eval(). It makes your program easily breakable, or worse... –  mrKelley May 21 '13 at 1:14
    
I've found my error. I should define the show function as def show(self, dummy, result): Independently if I pass just one argument to the function (like in self.show(result), it seems python passes two "invisible arguments": the first is "self", the instance of the class and the second is the method who called the function (the function on_button_pressed or the button objetct, i'm not sure). That's a bit confusing... –  David Sousa May 21 '13 at 1:37
add comment

I've found my error. I should define the show function as

def show(self, dummy, result):

Independently if I pass just one argument to the function (like in self.show(result), it seems python passes two "invisible arguments": the first is "self", the instance of the class and the second is the method who called the function (the function on_button_pressed or the button objetct, i'm not sure). That's a bit confusing...

the first and the second arguments are useless for me, but apparently I'm forced to declare them.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.