为什么我可以在 python 中乱序地编写一些内容,而其他内容则不能?
请看下面:
a = 5
print a + b
b = 4
当我尝试运行上面的代码时,出现错误: 回溯(最近一次调用最后一次): 文件“C:/Users/user/Documents/modules/ab.py”,第 2 行,位于 打印 a + b NameError: 名称 'b' 未定义
好的。 a + b
在定义 b
之前调用。这意味着 Python 按顺序运行代码,从上到下。 但是,这个怎么样:
class Data:
def __init__(self):
self.debug_level = 9
self.assign = [0, 0, 0, 0]
self.days = 0
def create_days(self, startTime, endTime):
res = 0
try:
if self.final_days < self.maximum_days:
上面, self.final_days
和 self.maximum_days
也还没有定义,但它没有给出任何错误。其背后的逻辑是什么?
此致,
Please have a look below:
a = 5
print a + b
b = 4
When I try to run the code above, it gives an error:
Traceback (most recent call last):
File "C:/Users/user/Documents/modules/ab.py", line 2, in
print a + b
NameError: name 'b' is not defined
Ok. a + b
is called before b
is defined. That means Python runs the code in order, starts from top to down.
But, how about this one:
class Data:
def __init__(self):
self.debug_level = 9
self.assign = [0, 0, 0, 0]
self.days = 0
def create_days(self, startTime, endTime):
res = 0
try:
if self.final_days < self.maximum_days:
Above, self.final_days
and self.maximum_days
are not defined yet either, but it does not give any errors. What is the logic behind it?
Best regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您实际上还没有“运行”代码。在您的示例中,您拥有的只是 Data 类中的方法声明。其中,Python 不会检查类字段是否存在,因为它们可能会在其他时间、在其他方法中设置(从这个意义上来说,Python 的类是可延展的)。
如果您尝试在 Data 类的新实例中运行 create_days 方法而不事先设置这些字段的值,您将收到错误。
You're not actually "running" the code yet. In your example, all you have is a method declaration inside the Data class. In it, Python will not check for the existence of class fields because they may be set at another time, in some other method (Python's classes are malleable in that sense).
If you try to run your create_days method in a new instance of the Data class without setting the values for those fields beforehand, you'll get an error.
它不会给出任何错误,因为定义类时不会访问属性。一旦你调用
create_days()
你就会遇到问题:DIt doesn't give any errors because the attributes are not accessed when the class is defined. As soon as you call
create_days()
you'll have a problem :D函数体仅在调用时计算,而不是在定义时计算。
The body of a function is evaluated only when it is called, not when it is defined.
仅当代码运行时才会查找引用。您可以在
create_days()
方法中放置任何您喜欢的名称,并且在执行包含它们的行之前不会检查任何名称。References are only looked up when the code is run. You can put whatever names you like in the
create_days()
method, and none will be checked until the line containing them is executed.如果你实际执行它,你会得到
AttributeError:数据实例没有属性“final_days”
要重现此问题:
此外,您还有一个
try
块。我认为这是其他一些代码的摘录。 try 块可能正在吞噬异常。If you actually executed it, you would get
AttributeError: Data instance has no attribute 'final_days'
To reproduce this:
also, you have a
try
block. I assume this is an excerpt from some other code. The try block is probably swallowing the exception.Python 是一种解释性语言,与 c++ 不同,它不会被编译,因此函数体在调用之前不会被求值。
Python is an interpreted language, unlike c++ it is not compiled so the body of a function isn't evaluated until it is called.