gimme that returned thing.

Wow, I am kind of an idiot sometimes. In my defense, I have been programming for maybe a year and have zero formal training. I’m pretty lucky to work with the people I work with, though, and I think my one saving grace is that I can usually tell when I’m going down the wrong path and I’m not particularly shy about asking for help before I find myself totally lost in the rabbit hole.

I’m sure everyone else knows this already, but hey, maybe someone is interested in the little shit that trips up Python newbies. So, here’s today’s adventure.


def do_something():
    foo = []
    foo.append("bar")
    return foo

if __name__ == "__main__":
    print foo

Obviously, that doesn’t work. I get a NameError: name ‘foo’ is not defined. But if I declare ‘foo’ outside the function, there’s no problem. Works just fine. I knew that was wrong, though, because no one else has to do that. I tried and tried to figure it out, because obviously I was having scope problems and it seemed like something that would be, well, obvious. But finally I broke down and asked someone, and he said, “wait, you’re not assigning the return value of that function?”

Um. In fact, no. So now I have this:


def do_something():
    foo = []
    foo.append("bar")
    return foo

if __name__ == "__main__":
    baz = do_something()
    print baz

And it works! Pretty lame as adventures go, I know, but it is kind of a major revelation to me.

Tags: ,

RSS feed | Trackback URI

5 Comments »

Comment by nat
2008-04-04 10:12:56

This actually prompted sort of an interesting thought yesterday. Could you (would you/should you?) have some kind of operator (I don’t mean in Python, just in general) that essentially popped a variable back up into the higher namespace? All of the languages I’ve used have been pretty strict about scope, I don’t know if there are others that play things a little looser. Could you have a language without return values at all? hmm, I think I need my own blog…

Comment by pam
2008-04-04 10:44:44

Do you mean, like, just declaring something to be global? Totally doable, but I feel our esteemed colleagues sending lightning bolts into my brain every time I even think about it.

(That said, I’m sure there are good reasons to do it on occasion, but my usual reason would be, “I can’t figure out a better way,” which is not good enough.)

Comment by nat
2008-04-04 13:18:18

Pam,
naw, I didn’t mean it in any practical sense. It was just an existential musing of sorts — life, the universe and everything, if you will. I’m not even suggesting that the compartmentalized, strict paradigm of namespace and scope that I’ve experienced isn’t a good thing, I’m just wondering if it’s possible for it to be any other way.

I think the idea was somewhat more half-baked than you might be giving me credit for.

 
 
Comment by Lindsey
2008-05-20 05:18:13

nat, the idea of such an operator sends a bit of chill down my spine. The consistent renaming problem is actually pretty hard even without this complication, and I think it would be really, really hard to implement it in a safe and predictable way. What if the name collides with a name the next level up? And so on.

Also, Pam, my Python is awful, but how about just

print do_something()

? It seems to me that it’s not so much about scope as it is that in the first example, the do_something() function just never gets called.

 
 
Comment by Paul Hummer
2008-04-04 12:00:14

Pam-

Variable scope was a hard thing for me to grasp when I first started programming. When I started working in PHP, it made things worse… :) It’s a difficult thing to explain to someone with very little programming experience, but a very valuable thing to understand, especially when you have a great language like python.

Paul

 
Comment by Pwhndvve
2008-08-09 11:52:42

Honi soit look closer buy cytotec meat steamed estivities.

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.

Trackback responses to this post