EX 43: Printing question

In doing my debugging work for 43 I was doing more complicated printing than what the video laid out. Most of it would print but some of it I could not print anything. This usually revolved around class execution…which I discovered (and probably should have realized already but didn’t until now) that when you do something like

print(foo.bar())

Actually executes foo.bar(). In this example it’s relatively easy to get around if foo.bar() has been instantiated, in which case:

x = foo.bar()

Means I can do this to get what I need

print(x)

But there were a couple of cases where I couldn’t figure out how to print out what I wanted without causing the code to execute itself…which obviously I didn’t want to happen since it means the code would execute twice; once in the print statement and again when that specific line was called in the code.

Take this abstract example…

class Thing():
     def __init__(self, data):
          self.data = data

     def foo(self, x):
          pass

     def bar(self):
          return self.foo(self.data)

(in the real world foo would do something with x but in order to make this as abstract as possible I put a pass statement in there)

The question I have is how could I print out what gets returned from bar? I can’t do this…

print("Returning:", self.foo(self.data))

All that would do is run the code. I don’t think it’s possible to instantiate it and print the instantiation.

Maybe there is no way to do it. But if that’s the case I need to know so I can drop this idea.

I’m afraid I don’t quite understand what you want to print.

Obviously you can’t print the return value of a function without executing it. And if you do want the return value, I think you’ve got the solution already:

def foo(bar):
    result = foobar(bar)
    print(result)
    return result

What exactly is the information you’re after?


If I may be so bold: Allow me to give you a few hints concerning the technical vocabulary. Please don’t be offended!! It’s just that you confused me a little with your wording.

“Instantiate” is used only with classes: You instantiate a class to get a single object of that class type, which is then called an “instance”:

instance = MyClass()

When you’re talking about return values from functions, you would rather use “bind”. The return value of foo() is bound to the variable x, or the other way around: x is set to the return value of foo().

x = foo()

Lastly, your problem is not about classes, and the expression “class execution” doesn’t really make a lot of sense. It’s about function execution.

1 Like

I’m not sure I get your use case. Can you elaborate? Are you trying to understand what a thing may return without executing a function? I assume it’s not debugging?

Perhaps read up on builtins __repr__ or __str__ and see if they help.

Well this does clear a few things up for me. And you can’t offend me. If my technical jargon is off, it’s off and mean it needs to be worked on some more.

1 Like

Excellent! This is the internet, you never know how things are going to be understood…

1 Like

I find that deeply offensive! :stuck_out_tongue:

As long as we’re talking about correct technical jargon how would we describe this?

handler = self.handlers[self.startState]

(fyi, self.handlers is a dictionary and self.startState is a key. The value in this key:value pair is a function)

Set handler to… oh that’s awkward… the value associated with, or mapped to self.startState in self.handlers…?

Edit: Better: From x get the value mapped to y and bind it to z.

1 Like
A free service run by Zed A. Shaw for learncodethehardway.org.