您实际上并没有在Machine
类中存储任何东西。你正在做唯一被存储在(容易混淆的名字命名)值carObject
:
from StoreCar import *
carObject = []
class Machine:
def calculation():
VehicleID = 2 # this is genarated Austomatically from system
#and increases every time a vehicle enters
Fuel = 15 # this is also calculated automatically from system.
# You're putting things in the `carObject` *list*, which
# should probably just be called `cars`
carObject.append(StoreCar(VehicleID,'car')
self.carObject[VehicleID-1].setFC(Fuel)
您的代码,在一般情况下,有可能是让你的生活更加困难,它需要有一个几个问题现在,并且肯定会让事情变得更糟。我是假设,你在某种类别,这是给予一些具体的约束作业,否则绝对没有理由做很多事情,你在做什么。
这里是我改变的东西:
from <module> import *
是非常很少,你想要做什么。只需import module
。或者,import super_long_annoying_to_type_module as slattm
并使用点访问。
- 您不需要需要 a
Machine
类,除非这是您的任务的参数之一。除了混淆你的代码之外,它没有做任何事情。 calculation
甚至不需要self
,所以要么用@classmethod
来装饰,要么只是一个函数。
- Python的命名约定 - 模块(文件),变量和函数/方法应该是
snake_cased
,类应该是StudlyCased
。这不会杀了你,但是这是一个你会在大多数其他Python代码中看到的约定,如果你遵循它,会让你的代码更容易被其他Python程序员读取。
cars.py
class StoreCar:
def __init__(self, id_,name):
self.id_ = id_
self.name= name
self.fc_list= []
# If you're *setting* the fuel capacity, it shouldn't be a list.
# (assuming that's what FC stands for)
def add_fuel(self, fuel):
self.fc_list.append(fuel)
factory.py
import cars
class Machine:
def __init__(self):
self.cars = []
# Assuming that the vehicle ID shouldn't
# be public knowledge. It can still be got
# from outside the class, but it's more difficult now
self.__vehicle_id = 0
def calculation(self):
self.__vehicle_id += 1
fuel = 15 # this is also calculated automatically from system.
car = cars.StoreCar(self.__vehicle_id, 'car')
# Typically, I'd actually have `fuel` as a parameter
# for the constructor, i.e.
# cars.StoreCar(self.__vehicle_id, 'car', fuel)
car.add_fuel(fuel)
self.cars.append(car)
somethingelse。PY
import factory
class SomeOtherClass:
def get_vehicle_data(self):
machine = factory.Machine()
machine.calculate()
print(machine.cars)
注意的是,如果我被任何一种分配不受约束的,我可能只是做这样的事情:
from collections import namedtuple
Car = namedtuple('Car', ('id', 'fuel_capacity', 'name'))
def gen_vehicle_ids():
id = 0
while True:
id += 1
yield id
vehicle_id = gen_vehicle_ids()
def build_car():
return Car(id=next(vehicle_id), name='car', fuel_capacity=15)
# If you don't want a namedtuple, you *could* just
# use a dict instead
return {'id': next(vehicle_id), 'type': 'car', 'fuel_capacity': 15}
cars = []
for _ in range(20): # build 20 cars
cars.append(build_car())
# an alternative approach, use a list comprehension
cars = [build_car() for _ in range(20)]
print(cars) # or do whatever you want with them.
对于你们之间可以用namedtuple方法做比较对字典的方法:
# dict approach
for car in cars:
print('Car(id={}, name={}, fuel_capacity={})'.format(
car['id'], car['name'], car['fuel_capacity']))
# namedtuple approach
for car in cars:
print('Car(id={}, name={}, fuel_capacity{})'.format(
car.id, car.name, car.fuel_capacity))
退房http://pyformat.info更多的字符串格式化TR icks。
我想要的是从外部类访问存储在机器类(车辆数据)中的数据。如果我不清楚请让我知道。谢谢@LogicStuff – DrakonianD
很难为你提供帮助,因为看起来你需要了解更多关于面向对象的知识,并重新思考你的程序的整个方案。此外,您应该[创建一个简化的示例](https://stackoverflow.com/help/mcve)(但包含您的主脚本),并避免谈论与您的问题无关的事情(例如,看起来我们不喜欢不需要了解代理号码或模拟器等背景信息......)。 – zezollo