0

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.

5
  • 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. Commented May 21, 2013 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. Commented May 21, 2013 at 0:23
  • The output you show look like you were instead passing in self explicitly somewhere: self.show(self). Commented May 21, 2013 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. Commented May 21, 2013 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. Commented May 21, 2013 at 1:00

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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...
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...
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.