Why can this run successfully ?
Excuse me, why can this run successfully , Take this one Battery Commented out , It can run successfully
The subject just derived a ElectricCar class , Although undefined is used in its constructor Battery, But this class is not instantiated , So this code doesn't really execute . about Python The interpreter says , Definition of function or class , Not executing code . Consider the following code , There will be no error when defining a function , An exception will pop up when calling the function .
>>> def haha(): return Battery()>>> b = haha()Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> b = haha() File "<pyshell#2>", line 2, in haha return Battery()NameError: name 'Battery' is not defined