为什么在仅导入 class 时代码在 class 定义 运行 之外?
Why is code outside a class definition run when only the class is imported?
预期
我想从另一个文件导入 python class,而不导入文件的其余部分。
问题
当我使用 from Exercise_7 import Vehicle
导入 class(车辆是 class),然后 运行 我的代码时,似乎来自 Exercise_7.py 的行在 class定义也运行.
代码
Exercise_7.py :
import numpy as np
class Vehicle:
def __init__(self, typeOfVehicle, velocity, colour, electric):
self.typeOfVehicle = str(typeOfVehicle)
self.velocity = tuple(velocity)
self.velocity = np.array(self.velocity)
self.colour = str(colour)
self.electric = bool(electric)
def updateVelocity(self, velocityTuple):
self.velocity = np.array(velocityTuple)
def allAttributes(self):
attributes = [self.typeOfVehicle, self.velocity, self.colour, self.electric]
print(attributes)
def emissions(self, distance):
#electric vehicles
if self.electric == True:
if self.typeOfVehicle == "Car":
emission = 1*distance
elif self.typeOfVehicle == "Truck":
emission = 5*distance
else: #vehicle is not electric
if self.typeOfVehicle == "Car":
emission = 10*distance
elif self.typeOfVehicle == "Truck":
emission = 50*distance
return emission
#---TEST CODE THAT IS BEING RUN (when I don't want it to)---
vehicle1 = Vehicle("Car", (3,5), "red", False)
vehicle1.updateVelocity((2,6))
vehicle1.allAttributes()
print(vehicle1.emissions(100))
Exercise_8.py(当运行,运行是不需要的代码的文件):
from Exercise_7 import Vehicle
import numpy as np
import random as r
def newVehicles(type, electric1):
colours = ["red", "blue", "green", "black", "grey"]
for i in range(0,50):
velocity = (r.uniform(1,70), r.uniform(1,70))
newVehicle = Vehicle(type, velocity, colours[r.randint(0,4)], electric1)
vehicles.append(newVehicle)
def createVehicles():
newVehicles("Car", True)
newVehicles("Car", False)
newVehicles("Truck", True)
newVehicles("Truck", False)
return vehicles
def calculateEmissions(vehicles, time):
electricCarsEmissions = 0
nonelectricCarsEmissions = 0
electricTrucksEmissions = 0
nonelectricTrucksEmissions = 0
for vehicle in vehicles:
speed = (vehicle.velocity[0]**2 + vehicle.velocity[1]**2)**1/2
distanceTravelled = speed*time
vehicle.distance = distanceTravelled
emission = vehicle.emissions(distanceTravelled)
if vehicle.typeOfVehicle == "Car" and vehicle.electric == True:
electricCarsEmissions += emission
elif vehicle.typeOfVehicle == "Car" and vehicle.electric == False:
nonelectricCarsEmissions += emission
elif vehicle.typeOfVehicle == "Truck" and vehicle.electric == True:
electricTrucksEmissions += emission
elif vehicle.typeOfVehicle == "Truck" and vehicle.electric == False:
nonelectricTrucksEmissions += emission
return electricCarsEmissions, nonelectricCarsEmissions, electricTrucksEmissions, nonelectricTrucksEmissions
vehicles = []
listOfVehicles = createVehicles()
print(calculateEmissions(listOfVehicles, 20))
当Exercise_8.py为运行时,输出:
['Car', array([2, 6]), 'red', False] #output from Exercise_7.py code
1000 #output from Exercise_7.py code
(1836161.109986278, 16586342.282856379, 7901210.428254995, 74406974.7703749) #desired output
这就是解释型语言的工作方式(如 python、javascript ...)任何存在于函数定义或 class 之外的代码都将由口译员(在你的例子中是 python 口译员)
模块是可执行代码块,def
和 class
语句也是如此。它们仅在 整个 代码上下文中得到明确定义。因此,不可能只加载模块的一部分。
运行 from Exercise_7 import Vehicle
仍然需要 加载并执行 整个模块 Exercise_7
到解释器中,即使它只是 导入并绑定名称Vehicle
到文件中。
在常规导入期间不应该 运行 的代码应该在 __main__
guard 中:
if __name__ == "__main__":
vehicle1 = Vehicle("Car", (3,5), "red", False)
vehicle1.updateVelocity((2,6))
vehicle1.allAttributes()
print(vehicle1.emissions(100))
松散地说,一个模块只有在直接执行时才是 __main__
——例如as python -m Exercise_7
– 但不是在被另一个模块导入时。
预期
我想从另一个文件导入 python class,而不导入文件的其余部分。
问题
当我使用 from Exercise_7 import Vehicle
导入 class(车辆是 class),然后 运行 我的代码时,似乎来自 Exercise_7.py 的行在 class定义也运行.
代码
Exercise_7.py :
import numpy as np
class Vehicle:
def __init__(self, typeOfVehicle, velocity, colour, electric):
self.typeOfVehicle = str(typeOfVehicle)
self.velocity = tuple(velocity)
self.velocity = np.array(self.velocity)
self.colour = str(colour)
self.electric = bool(electric)
def updateVelocity(self, velocityTuple):
self.velocity = np.array(velocityTuple)
def allAttributes(self):
attributes = [self.typeOfVehicle, self.velocity, self.colour, self.electric]
print(attributes)
def emissions(self, distance):
#electric vehicles
if self.electric == True:
if self.typeOfVehicle == "Car":
emission = 1*distance
elif self.typeOfVehicle == "Truck":
emission = 5*distance
else: #vehicle is not electric
if self.typeOfVehicle == "Car":
emission = 10*distance
elif self.typeOfVehicle == "Truck":
emission = 50*distance
return emission
#---TEST CODE THAT IS BEING RUN (when I don't want it to)---
vehicle1 = Vehicle("Car", (3,5), "red", False)
vehicle1.updateVelocity((2,6))
vehicle1.allAttributes()
print(vehicle1.emissions(100))
Exercise_8.py(当运行,运行是不需要的代码的文件):
from Exercise_7 import Vehicle
import numpy as np
import random as r
def newVehicles(type, electric1):
colours = ["red", "blue", "green", "black", "grey"]
for i in range(0,50):
velocity = (r.uniform(1,70), r.uniform(1,70))
newVehicle = Vehicle(type, velocity, colours[r.randint(0,4)], electric1)
vehicles.append(newVehicle)
def createVehicles():
newVehicles("Car", True)
newVehicles("Car", False)
newVehicles("Truck", True)
newVehicles("Truck", False)
return vehicles
def calculateEmissions(vehicles, time):
electricCarsEmissions = 0
nonelectricCarsEmissions = 0
electricTrucksEmissions = 0
nonelectricTrucksEmissions = 0
for vehicle in vehicles:
speed = (vehicle.velocity[0]**2 + vehicle.velocity[1]**2)**1/2
distanceTravelled = speed*time
vehicle.distance = distanceTravelled
emission = vehicle.emissions(distanceTravelled)
if vehicle.typeOfVehicle == "Car" and vehicle.electric == True:
electricCarsEmissions += emission
elif vehicle.typeOfVehicle == "Car" and vehicle.electric == False:
nonelectricCarsEmissions += emission
elif vehicle.typeOfVehicle == "Truck" and vehicle.electric == True:
electricTrucksEmissions += emission
elif vehicle.typeOfVehicle == "Truck" and vehicle.electric == False:
nonelectricTrucksEmissions += emission
return electricCarsEmissions, nonelectricCarsEmissions, electricTrucksEmissions, nonelectricTrucksEmissions
vehicles = []
listOfVehicles = createVehicles()
print(calculateEmissions(listOfVehicles, 20))
当Exercise_8.py为运行时,输出:
['Car', array([2, 6]), 'red', False] #output from Exercise_7.py code
1000 #output from Exercise_7.py code
(1836161.109986278, 16586342.282856379, 7901210.428254995, 74406974.7703749) #desired output
这就是解释型语言的工作方式(如 python、javascript ...)任何存在于函数定义或 class 之外的代码都将由口译员(在你的例子中是 python 口译员)
模块是可执行代码块,def
和 class
语句也是如此。它们仅在 整个 代码上下文中得到明确定义。因此,不可能只加载模块的一部分。
运行 from Exercise_7 import Vehicle
仍然需要 加载并执行 整个模块 Exercise_7
到解释器中,即使它只是 导入并绑定名称Vehicle
到文件中。
在常规导入期间不应该 运行 的代码应该在 __main__
guard 中:
if __name__ == "__main__":
vehicle1 = Vehicle("Car", (3,5), "red", False)
vehicle1.updateVelocity((2,6))
vehicle1.allAttributes()
print(vehicle1.emissions(100))
松散地说,一个模块只有在直接执行时才是 __main__
——例如as python -m Exercise_7
– 但不是在被另一个模块导入时。