应该在python中使用函数之前进行定义? [英] should function be defined before it is used in python?

查看:176
本文介绍了应该在python中使用函数之前进行定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该在使用之前定义函数?但为什么下面的代码可以工作:

$ $ $ $ $ code $ def main():
dog()

)def dog():
print(This is a dog。)

if __name__ =='__main__':
main()

我的意思是dog()是在它被调用后定义的,它是如何工作的?

解决方案

在函数调用之前,函数/方法块内的代码不会执行。而CPython语言实现(这是最常见的和你可能使用的)在编译时不会像C这样的语言进行名称检查。因为它主要是一种解释型语言,它会检查名称是否可用命名空间动态地在运行时。



另外你应该知道关于Python的一点是类内的代码是在创建类时执行的,但是在方法/函数中的代码不是。一个 def 语句只是将该函数的名称添加到名称空间。它不会在函数本身内运行任何代码,直到函数被调用。



请考虑下列合法的Python代码。

  class Something:
def __init __(self):
self.puppy = Dog()

class Dog:
def __init __(self):
pass

inst = Something()

这是完全合法的,因为 __ init__ 不会在 Dog 类被定义之前运行。但是如果我们把这个构造函数移到类Dog的创建之上呢?



这段代码会抱怨NameError,因为Dog还没有定义!

  class事物:
def __init __(self):
self.puppy = Dog()

inst = Something()
$ b $ class Dog:
def __init __(self):
pass

最后一个例子......

这段代码会抱怨同样的NameError。

  class Something:
my_puppy = Dog()
def __init __(self):
self.puppy = Dog()

class Dog:
def __init __(self):
pass

inst = Something()
Something
中的代码都会立即执行,并且在执行时,名称空间/ scope不能访问名称 Dog


should the functions be defined before it is used? but why the following code works:

def main():
    dog()

def dog():
    print("This is a dog.")

if __name__ == '__main__':
    main()

I mean the dog() is defined after it is called, how this works?

解决方案

Code within the blocks of functions/methods does not execute before the functions are called. And the CPython language implementation (that's the most common one and the one you are probably using) does not do name-checking at compile time like languages such as C. Since it is primarily an interpreted language, it checks if names are available in the namespace dynamically at runtime.

Furthermore something you should know about Python is that code within classes is executed at the time of class creation, but code with in methods/functions is not. A def statement simply adds the name of the function to the namespace. It does not run any code within the function itself until the function is called.

Consider the following legal Python code.

class Something:
    def __init__(self):
        self.puppy = Dog()

class Dog:
    def __init__(self):
        pass

inst = Something()

This is perfectly legal because __init__ is not run before Dog class gets defined. But what if we moved that constructor above the creation of class Dog?

This code would complain about a NameError because Dog isn't defined yet!

class Something:
    def __init__(self):
        self.puppy = Dog()

inst = Something()

class Dog:
    def __init__(self):
        pass

One final example...

This code would complain about the same NameError.

class Something:
    my_puppy = Dog()
    def __init__(self):
        self.puppy = Dog()

class Dog:
    def __init__(self):
        pass

inst = Something()

This is because all of the code inside Something is immediately executed and at that point in the execution, the namespace/scope does not have access to the name Dog.

这篇关于应该在python中使用函数之前进行定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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