2

In other languages I often do something like this:

someFunc()

someFunc() {
  // This is my function
}

This way I can stack all my functions lower in the file, but make the function calls at the top. Now I have a nice overview of everything that's happening.

However, when I did this in Python 3 in Spyder, I got the warning that Undefined name: 'myfunc'

my_func("Some string")

def my_func(some_var):
  print(some_var)

The code works fine, but I'm not sure what best practice here. Are there any negative impacts caused by my method? Or is it merely a guideline to have your functions before you call them?

The code I set above does work for me. Why is that? I'm running Python 3.4.3 with Anaconda. What's different about my version? Or is it because I run it in Spyder?

Edit: so apparently Spyder works in mysterious ways. First I had the call after the definition, which worked, then I swapped the call to the first line and it still worked. Spyder seems to cache functions or at least not flushing them out. (Though I'm not sure if it's Spyder that's doing the caching or Python itself. I'm assuming Python.) For any newbies out there wondering about this: solution is to restart your programme and/or Python service.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • *In other languages* unlike python which is interpreted. It does not have the ability to "look forward" in search of functions. – Bhargav Rao Aug 27 '15 at 17:14
  • @BhargavRao But why, then, does it work? – Bram Vanroy Aug 27 '15 at 17:15
  • 1
    It's important to realize that `def my_func(some_var)` is not a compiler directive. It's an executable statement that creates a function and binds it to a name. – Steven Rumbalski Aug 27 '15 at 17:16
  • 2
    @BramVanroy It does not work in standard python. – Bhargav Rao Aug 27 '15 at 17:16
  • 2
    Downvoters should explain *why* this is a bad question. – jds Aug 27 '15 at 17:17
  • 1
    Note that `def foo(): bar()` followed by `def bar(): pass` works because `bar` isn't resolved until `foo` is called. – Steven Rumbalski Aug 27 '15 at 17:18
  • 2
    To add to @StevenRumbalski's comment: the order in which you define the methods of a class doesn't matter either, for the same reason. – Wander Nauta Aug 27 '15 at 17:19
  • @gwg Didn't downvote, but it's a bad question because the code doesn't work. – Markus Meskanen Aug 27 '15 at 17:21
  • @gwg Beats me. I tried the question on CodeReview, but that didn't work out. So I posted it here. Apparently not welcome I either. – Bram Vanroy Aug 27 '15 at 17:21
  • @Bram Don't care much about the downvotes. Your edit might make them revoke it. I am installing anaconda to test out your problem. – Bhargav Rao Aug 27 '15 at 17:22
  • 1
    @BramVanroy: Although you say it works, I don't believe you. There is something extra going on. I'll bet the code was not exactly the same across environments. – Steven Rumbalski Aug 27 '15 at 17:22
  • 2
    @BramVanroy The code, as is, doesn't work. You've most likely defined `my_func` earlier on already in the same REPL session, try restarting your IDLE or w/e you're using. The code doesn't work unless you've done something else already. – Markus Meskanen Aug 27 '15 at 17:22
  • 1
    And still, it does not really matter whether it works or not, the question is about the why so it's still interesting, and can be of use to other people learning the language. I guess rewording the question title would help towards getting less downvotes, though. – spectras Aug 27 '15 at 17:23
  • Highly related and must read [Is it possible to forward-declare a function in Python?](http://stackoverflow.com/questions/1590608/is-it-possible-to-forward-declare-a-function-in-python) – Bhargav Rao Aug 27 '15 at 17:27
  • @BhargavRao correct be if I am wrong python is not a [interpreted language](http://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files) – The6thSense Aug 27 '15 at 17:27
  • @Vignesh Did you read the accepted answer there? It clearly mentions *Python is an interpreted language*. – Bhargav Rao Aug 27 '15 at 17:29
  • @MarkusMeskanen, if you see Bram's edit, it turns out Spyder was causing the issue, so it was working for him. Rather than just immediately downvote because we don't understand something, maybe we should ask for clarifying details first? – jds Aug 27 '15 at 17:29
  • @BhargavRao the highly voted answer says the other way right if my english is right – The6thSense Aug 27 '15 at 17:30
  • @gwg Like I said already, I didn't downvote him. You're barking at the wrong tree, I simply explained why some people downvoted him; because his doesn't work as is. – Markus Meskanen Aug 27 '15 at 17:31
  • 1
    @MarkusMeskanen, not barking, just trying to make this a more welcoming place. – jds Aug 27 '15 at 17:33
  • @gwg I could be wrong here since I'm not a native, but I think "barking up the wrong tree" simply means you must've made a mistake and you're talking to the wrong person about your issue. Didn't mean it offensively, just wanted to point out that I'm not the one you should be talking to :) – Markus Meskanen Aug 27 '15 at 17:35
  • 1
    @Vignesh I suppose you are confused as there is an intermediate compilation step. Do go through the other answers. If there a need be do ask another question as comments aren't the right place to discuss. – Bhargav Rao Aug 27 '15 at 17:38
  • @MarkusMeskanen, ha, you used it correctly! To use another phrase, I think we're on the same page. – jds Aug 27 '15 at 17:38
  • 1
    You could improve your question by working "Spyder" into the title. – Steven Rumbalski Aug 27 '15 at 17:41
  • 1
    @StevenRumbalski - I would recommend against having tags in the title. Spyder was already mentioned in the original question body. If it needs to be emphasized, it should be added as a tag. – TigerhawkT3 Aug 27 '15 at 17:49
  • 1
    @TigerhawkT3: It's important because it's not really a question about Python behavior. Python does not support forward declaration. The title, as currently worded, includes the assumption that it does. That mistaken assumption was probably the driver of the initial downvotes. Leaving the title as-is allows the misleading information to remain. – Steven Rumbalski Aug 27 '15 at 17:53
  • 1
    I didn't say it wasn't important. It is, in fact, very important. However, tags do not belong in titles. In fact, I am now noticing that the current title includes the "Python" tag, which I will edit out promptly. The downvoters should have read the question more carefully, because it did indeed include the necessary information. – TigerhawkT3 Aug 27 '15 at 17:55

2 Answers2

6

That doesn't work in the standard Python build, because the file is parsed in order. The other languages you refer to are compiled (in the traditional sense, not JIT or anything), so the order doesn't matter, but Python requires the first things to come first.

Define/assign/create/etc. things before using them.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • Please take my edit (last two paragraphs) into account. I'm trying to figure out why this is working in my case. – Bram Vanroy Aug 27 '15 at 17:20
  • 3
    @BramVanroy Try restarting your interpreter, maybe you've accidentally defined `my_func` earlier on already? – Markus Meskanen Aug 27 '15 at 17:21
  • 1
    Note my mention of the _standard_ Python build. – TigerhawkT3 Aug 27 '15 at 17:22
  • which is standard? jpython? pypy? cpython? some other? – spectras Aug 27 '15 at 17:24
  • 2
    @spectras Quite obviously CPython, the one used by a huge majority. From a quick google: "CPython is the original Python implementation. It is the implementation you download from Python.org.". It might not be called the "standard", but you surely understood what he meant. – Markus Meskanen Aug 27 '15 at 17:25
  • The standard build is [the first result when you Google "download python"](https://www.python.org/downloads/). I wouldn't recommend anything but that one, for reasons like this. – TigerhawkT3 Aug 27 '15 at 17:27
  • @MarkusMeskanen Thank you. See the edit to my first post. That was the issue. – Bram Vanroy Aug 27 '15 at 17:27
1

From my own comment:

Try restarting your interpreter, maybe you've accidentally defined my_func earlier on already?

And another one:

The code, as is, doesn't work. You've most likely defined my_func earlier on already in the same REPL session, try restarting your IDLE or w/e you're using. The code doesn't work unless you've done something else already.

As it turned out, this was the issue, and your IDE was somehow "caching" the function definition from earlier on during that session.

In general, when you usually run into weird problems that make no sense and shouldn't be there in the first place (such as this), you should restart your IDE. More often than not it solves the problem, and the issue was just something silly like caching.

Also, when using CPython, the default, most widely used implementation of the Python programming language, you're using Python as an interpreted language, thus your interpreter is going through the code in order from top to bottom. This is why you normally can't call a function before defining it in Python.

Community
  • 1
  • 1
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119