为什么我可以在定义它之前调用一个函数,只有一个警告? [英] Why can I call a function before defining it, with only a warning?

查看:147
本文介绍了为什么我可以在定义它之前调用一个函数,只有一个警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其他语言中,我经常这样做:

  someFunc()

someFunc ){
//这是我的函数
}

这样我就可以将文件中的所有函数放在较低位置,但将函数调用放在顶部。现在我对所发生的一切有了一个很好的概述。

然而,当我在Spyder的Python 3中完成这项工作时,我收到了警告: Undefined name:' myfunc'

  my_func(Some string)

def my_func(some_var) :
print(some_var)

代码正常工作,但我不确定这里最好的做法。我的方法是否会产生负面影响?或者,它只是一个准则,让你的功能在你打电话给他们之前?



上面设置的代码确实对我有用。这是为什么?我使用Anaconda运行Python 3.4.3。我的版本有什么不同?或者是因为我在Spyder中运行它?



编辑:显然,Spyder以神秘的方式工作。首先,我在定义之后接通了电话,然后将呼叫交换到第一行,它仍然有效。 Spyder似乎有一些缓存功能,至少不会将其清除。 (虽然我不确定Spyder是在做缓存还是Python本身,但我假设Python)。对于那些想知道这个问题的新手们:解决方案是重新启动程序和/或Python服务。

解决方案

From


尝试重新启动您的解释器,也许你已经在早些时候意外地定义了 my_func


另一个: p>


代码不起作用。您很可能早先在同一REPL会话中定义了 my_func ,请尝试重新启动您的IDLE或正在使用的w / e。该代码不起作用,除非你已经做了其他事情。


事实证明,这是问题,而你的IDE在某种程度上缓存了该会话期间的函数定义。



一般来说,当您经常遇到一些毫无意义且不应该首先(比如这个),你应该重新启动你的IDE。通常情况下,它解决了这个问题,这个问题就像缓存一样愚蠢。



另外,当使用CPython时, Python编程语言的默认,最广泛使用的实现,您使用Python作为解释型语言,因此您的解释器正在通过代码从上到下依次排列。这就是为什么在用Python定义它之前通常不能调用函数的原因。


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.

解决方案

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.

这篇关于为什么我可以在定义它之前调用一个函数,只有一个警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆