为什么这段代码没有语法错误?

Why Is There No Syntax Error In This Code?

我忘记在 'dog.bark' 末尾添加括号来调用该方法,但是,它不会引发语法错误。这是为什么?

class Dog:
  sound = "Woof"

  def __init__(self, age):
   self.age = age

  def bark(self):
   print(self.sound)

dog = Dog(27)
dog.bark

SyntaxError:

Syntax errors are produced by Python when it is translating the source code into byte code. They usually indicate that there is something wrong with the syntax of the program.

只是 dog.bark 将引用该函数。这是完全有效的。有时,您一定会收到一些奇怪的消息,告诉内存位置的某个函数名称。那是因为它打印了被引用但未被调用的函数。

Python 将像往常一样简单地解释它。语法没问题