如何在课程范围内使用课程名称? [英] How to use class name in class scope?

查看:87
本文介绍了如何在课程范围内使用课程名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Python不太熟悉。因此,我在编码时会遇到一些问题。

I'm not very familiar with Python. So I have some problem while I code.

功能块中使用功能名称是很正常的,例如:

It's very normal to use the function name in the function block, for example:

def factorial(n):
    if n == 1:
        return n
    else:
        return n * factorial(n-1)

但是我尝试在 class块中使用类名,但出现了问题:

But when I try to do use the class name in the class block, things go wrong:

class Foo(object):
    a = Foo
NameError: name 'Foo' is not defined

尽管下面的代码还可以:

Although the code below is okay:

class Foo(object):
    def __init__(self):
        a = Foo

然后我使用<$调试这两个代码c $ c> print globals()语句。我发现 class块中的全局变量dict不包含 class Foo ,而 __ init __ 功能块包含它。

Then I debug these two codes using print globals() statement. I found that the global variable dict in class block doesn't contain class Foo while the global variable dict in __init__ function block contains it.

因此,似乎类名绑定是在执行 class之后和执行功能块之前。

So it seems that the class name binding is after the execution of class block and before the execution of function block.

但是我不喜欢编码基础领域的猜测。有人可以对此提供更好的解释或官方资料吗?

But I don't like guesswork in foundation area of coding. Could anyone offer a better explanation or official material about this?

推荐答案

您的解释正确:


类名称绑定是在执行类块之后且在执行功能块之前。

the class name binding is after the execution of class block and before the execution of function block.

这只是说立即执行一个类块,而直到调用该函数才执行一个功能块。请注意,在这两种情况下,直到创建对象(类或函数)后,名称才被绑定。只是函数主体是在创建函数后之后执行的,而类主体是在创建类之前 执行的(作为类创建过程的一部分)。

This is just saying that a class block is executed right away, while a function block is not executed until the function is called. Note that in both cases the name is not bound until after the object (class or function) is created; it's just that function bodies are executed after the function is created, while class bodies are executed before the class is created (as part of the class creation process).

这是因为类和函数是不同的野兽:当您定义一个类时,您正在立即定义该类应包含的内容(即,其方法和属性);当您定义函数时,您正在定义稍后发生的事情(当您调用它时)。

This is because classes and functions are different beasts: when you define a class, you are defining "right now" what the class should contain (i.e., its methods and attributes); when you define a function, you are defining what is to happen at some later point (when you call it).

文档明确指出:


类定义是可执行语句。它首先评估继承列表(如果存在)。 [...]然后执行类的套件[...]

A class definition is an executable statement. It first evaluates the inheritance list, if present. [...] The class’s suite is then executed [...]

类主体在<$时执行c $ c> class 语句被执行。这与某些其他语言不同,在其他语言中,类定义是声明,不会像其他语句那样以源文件的线性顺序执行。 (我相信很明显,为什么在定义函数时不执行函数主体 -如果立即将代码放入函数中,没有什么意义。)

The class body is executed at the time the class statement is executed. This is different from some other languages, where a class definition is a "declaration" which is not executed in the linear order of the source file like other statements. (I trust it's obvious why the function body is not executed when the function is defined -- there wouldn't be much point in putting code in a function if it just ran right away.)

这篇关于如何在课程范围内使用课程名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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